Jack's Blog

流淌的心,怎能阻拦,吹来的风,又怎能阻挡。

ACM--C++初学

Jacob posted @ Sep 16, 2017 09:59:19 PM in 未分类 with tags acm C++ , 1895 阅读

       对于一个存在着标准输入输出的C++控制台程序,一般会在#include <iostream>的下一行发现一句话,using namespace std

       这句话其实就表示了所有的标准库函数都在标准命名空间std中进行了定义。其作用就在于避免发生重命名的问题。

#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;   
}

上例中的“ZhangSan::a”和“LiSi::a”分别表示了调用张三命名空间中的a变量和李四命名空间中的a变量。这样的好处显而易见,那就是虽然张三和李四这两个程序员都定义了一个变量a,但是并不会出现重名的危险。

 2. 关于using namespace *

  顾名思义,using namespace * 就表示释放命名空间* 中间的东西。好处在于我们在程序里面就不用在每个函数的头上都加上*::来调用。比如说如果上面那个程序,如果我们不在using namespace std,那么我们就需要在主函数中的标准输出流cout函数前面加上std,写成
 
std::cout
表示调用std空间里面的标准输出流cout。但是有些时候我们也不能图这个方便,比如说如果在主函数中将命名空间ZhangSan和LiSi的中所定义的变量释放出来,如下例1:
#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;
}
如果我们在主函数中把 int a=1给删除,如下例2:
#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;
}
会发现根本就不会通过编译,输出的错误信息为:
error C2872: “a”: 不明确的符号
  分析可以看出,上面这个例2会引起歧义。因为ZhangSan中间的a被释放出来,同理LiSi中间的a也被释放出来了。那么编译器就不知道到底哪个才是需要输出的a,自然就会引起歧义了。同理,在例1中,编译器同样不知道到底哪个才是需要输出的a,于是它只采用了主函数中自己定义的a,这样程序也不会报错,但是只会输出1,自然结果就如上面的图所示了。
摘自:http://www.cnblogs.com/uniqueliu/archive/2011/07/10/2102238.html

 

Avatar_small
Punjab +1 Model Pape 说:
2022年8月22日 02:15

There are several public and private schools in Punjab that are associated with this board; all of these schools operate under its supervision and are also controlled by it. Many students from this state took the class 11th exams this year as well, and they are all currently waiting for the Punjab +1 PSEB 11th Question Paper 2023 of these exams. Punjab +1 Model Paper 2023 Punjab Board will as soon as possible announce the Punjab +1 Important Question Paper 2023 for class 11th via their official website. Numerous pupils took the class 11th exams that were administered by this board in the previous academic year, which was done in the past. In this test, girls performed better than guys.

Avatar_small
Emma 说:
2023年1月25日 18:48

Congratulations on taking the first step to learning C++! If you are a beginner in the world of programming, C++ can be a great place to start. As one of the oldest and most popular programming languages, C++ has many advantages real property management Elkhart County that make it perfect for those just starting out. Its syntax is relatively easy to understand, and its massive library of resources makes it a great platform for advancing your coding skills. With the help of the ACM, you will have access to a number of resources to help you on your journey to becoming a C++ programmer. Good luck!


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter