为什么使用命名空间?
避免名字冲突
例:
#include <iostream>
#include <Windows.h>
#include <string>
//使用标准库中的命名空间std
//这里指定好以后,程序之后出现的string/cout/endl等名称都是指std中的名称
using namespace std;
namespace China {
float population = 14.1; //单位: 亿
string capital = "北京";
}
namespace Japan {
float population = 1.7; //单位: 亿
string capital = "日本";
}
//使用上面指定的命名空间China
//这里指定好以后,程序之后出现的capital/population等名字都是指China中的名称
using namespace China;
int main(void) {
cout << "Namespace" << endl;
cout << "首都:" << capital << endl;
cout << "人口:" << population << endl;
system("pause");
return 0;
}
原文:https://www.cnblogs.com/lvcunda/p/12114344.html