cout和std::cou区别

最近在读C++ Primer Fourth Edition。偶然发现cout和std::cout很有意思。一个是需要引用iostream.h,而另一个需要调用iostream。但他们的功能却又差不多。

当你使用std::时,则表明你在告诉编译器你正在调用标准命名空间(namespace standard)。下面将列举出几种调用cout的例子:

1)

view plaincopy to clipboardprint?

   1. #include <iostream>  
   2.   
   3. int main()  
   4. {  
   5.     using std::cout;    using std::endl;  
   6.       
   7.     cout << "Hello, world" << endl;  
   8.       
   9.     return 0;  
  10. }  

#include <iostream> int main() { using std::cout; using std::endl; cout << "Hello, world" << endl; return 0; }

2)

view plaincopy to clipboardprint?

   1. #include <iostream>  
   2.   
   3.     using namespace std;  
   4.      
   5. int main()  
   6. {  
   7.   
   8.     cout << "Hello, world" << endl;  
   9.   
  10.     return 0;  
  11. }  

#include <iostream> using namespace std; int main() { cout << "Hello, world" << endl; return 0; }

3)

view plaincopy to clipboardprint?

   1. #include <iostream>  
   2.   
   3. int main()  
   4. {  
   5.     std::cout << "Hello, world" << std::endl;  
   6.   
   7.     return 0;  
   8. }  

#include <iostream> int main() { std::cout << "Hello, world" << std::endl; return 0; }

至于这三种形式的区别,那就属于个人风格的问题了。

至于#include <iostream>和#include <iostream.h>,后者并非标准的C头文件。并且它指向的是整个iostream库,而前者则需要我们制定命名空间,从而避免了重复名称的混乱
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值