C++命名空间

                C++命名空间

  名称空间支持是一项C++特性,就是让您编写大型程序以及将多个厂商现有的代码组合起来的程序时更容易,它还有助于组织程序。名称空间让厂商能够将其产品封装在一个叫做名称空间的单元中,这样就可以用名称空间的名称来指出想使用哪家厂商的产品。

  例如:使用两家公司的产品,而他们都包含一个名称为wanda()的函数,这样,使用wanda()函数时,编译器将不知道指的是哪家公司的代码,所以C++引入了命名空间的概念。

命名空间的使用方法:

 1 //方式一
 2 
 3 #include <iostream>
 4 
 5 //iostream 提供了一个叫命名空间的东西,标准的命名空间是 std
 6 int main(void) {
 7   
 8   std::cout << "hello world" << std::endl;
 9 
10   return 0;
11 }

 

//方式二

 1 #include <iostream>
 2 using std::cout;    //声明命名空间中的一个变量
 3 using std::endl;    
 4 using std::cin;
 5 
 6 int main(void) {
 7   cout << "hello world" << endl;
 8 
 9   return 0;
10 }

 

//方式三

1 #include <iostream>
2 using namespace std;
3 
4 int main(void) {
5   cout << "hello world" << endl;
6 
7   return 0;
8 }

 

 

                                           学习,遇见更好的自己!!

 

posted @ 2019-11-14 09:47  苦行僧^_^  阅读(148)  评论(0)    收藏  举报