For a C + + console program that has standard input output, a sentence is typically found on the next line of # include <iostream>, using namespace Std. In fact, all of the standard library functions are defined in standard namespace Std. Its role is to avoid the problem of renaming.
1. About namespace C + + introduces a namespace namespace mainly solves the phenomenon that many programmers have the same name as the functions that may occur in writing the same project. The solution is to add your own namespace. For example, the following:
12345678910111213141516171819 |
#include <iostream>
using
namespace
std;
namespace
ZhangSan
{
int a=10;
//张三把10赋值给了变量a
}
namespace
LiSi
{
int
a=5;
//李四把10赋值给了变量a
}
void
main()
{
int a=1;
cout<<
"张三定义的a="
<<ZhangSan::a<<endl;
cout<<
"李四定义的a="
<<LiSi::a<<endl;
cout<<
"主函数定义的a="
<<a<<endl;
}
|
The "zhangsan::a" and "lisi::a" in the previous example respectively represent a variable in the call to the a variable in the Zhang San namespace and a in the John Doe namespace. The obvious benefit is that while both Zhang San and John Doe have defined a variable a, there is no danger of duplicate names. The result of the operation is: 2. With regard to the using namespace * As the name implies, the using namespace * represents the release of the middle of the namespace. The advantage is that we do not have to add *:: To the head of each function in the program. If, for example, the above program, if we are not using namespace STD, then we need to precede the standard output stream cout function in the main function with STD, written as
Represents the standard output stream cout in a call to STD space. But sometimes we can't figure this out, for example, if the variables defined in the namespace Zhangsan and Lisi are released in the main function, as in Example 1:
12345678910111213141516171819 |
#include <iostream>
using
namespace
std; namespace
ZhangSan
{
int
a=10;
//张三把10赋值给了变量a
}
namespace
LiSi
{
int
a=5;
//李四把10赋值给了变量a
} void
main()
{
int
a=1;
using
namespace
ZhangSan;
using
namespace
LiSi;
cout<<a<<endl;
}
|
The output of this program is: if we delete the int a=1 in the main function, the following example 2:
123456789101112131415161718 |
#include <iostream>
using
namespace
std;
namespace
ZhangSan
{
int
a=10;
//张三把10赋值给了变量a
}
namespace
LiSi
{
int
a=5;
//李四把10赋值给了变量a
}
void
main()
{
using
namespace
ZhangSan;
using
namespace
LiSi;
cout<<a<<endl;
}
|
You will find that it is not compiled, the output error message is: Error C2872: "A": ambiguous symbolic analysis can be seen above this example 2 will cause ambiguity. Because a in the middle of the Zhangsan is released, the same Lisi in the middle of a is released. Then the compiler does not know exactly which one is required to output a, nature will cause ambiguity. Similarly, in Example 1, the compiler also does not know exactly which one is required to output a, so it only uses the main function in its own definition of a, so that the program will not error, but only output 1, the natural result as shown in the figure above. See: http://www.cnblogs.com/uniqueliu/archive/2011/07/10/2102238.html
"Go" about "using namespace Std"