C++第三天_标准名称空间

才发现以前写的代码都太繁琐,有一些更简单的写法..也许看到前边的代码.很多程序员都会笑话我!没有办法,新手.

今天就学了标准名称空间,抱歉,由于和前边的代码几乎一样,所以没有写注释了.

下载: using.txt
  1. #include <iostream>
  2. int main()
  3. {
  4. using std::cout;
  5. using std::endl;
  6.  
  7. cout <<"hello there.\n";
  8. cout <<"here is 5: " << 5 << "\n";
  9. cout <<"the manipulator endl";
  10. cout <<"writres a new line to the screen." <<endl;
  11. cout <<"here is a very big number:\t" << 7000 << endl;
  12. cout <<"here is the sum of 8 and 5:\t";
  13. cout <<8+5 <<endl;
  14. cout <<" a u:\t\t\t\t";
  15. cout <<(float) 5/8 <<endl;
  16. cout <<"This is a big number: \t\t";
  17. cout <<(double) 7000*7000 << endl;
  18. char response;
  19. std::cin >> response;
  20. return 0;
  21. }

我做了一个测试,将using写到大括号里外的效果都是一样的.

下载: using.txt
  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::endl;
  5.  
  6. int main()
  7. {
  8. cout <<"hello there.\n";
  9. cout <<"here is 5: " << 5 << "\n";
  10. cout <<"the manipulator endl";
  11. cout <<"writres a new line to the screen." <<endl;
  12. cout <<"here is a very big number:\t" << 7000 << endl;
  13. cout <<"here is the sum of 8 and 5:\t";
  14. cout <<8+5 <<endl;
  15. cout <<" a u:\t\t\t\t";
  16. cout <<(float) 5/8 <<endl;
  17. cout <<"This is a big number: \t\t";
  18. cout <<(double) 7000*7000 << endl;
  19. char response;
  20. std::cin >> response;
  21. return 0;
  22. }

下边和上边的代码有一些地方不一样,但是一样可以编译,并且编译出来的效果是完全一样的.

下边一段代码是使用关键字namespace,效果依然一样

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. cout <<"hello there.\n";
  8. cout <<"here is 5: " << 5 << "\n";
  9. cout <<"the manipulator endl";
  10. cout <<"writres a new line to the screen." <<endl;
  11. cout <<"here is a very big number:\t" << 7000 << endl;
  12. cout <<"here is the sum of 8 and 5:\t";
  13. cout <<8+5 <<endl;
  14. cout <<" a u:\t\t\t\t";
  15. cout <<(float) 5/8 <<endl;
  16. cout <<"This is a big number: \t\t";
  17. cout <<(double) 7000*7000 << endl;
  18. char response;
  19. std::cin >> response;
  20. return 0;
  21. }

About the Author

doo

独立,内向,叛逆而勤于思索... 深知自己一直是一个无法融入社会的异类,自从有了孩子以后才感觉人有的时候真是身不由己.最大的理想是成为优秀的CGer.

Leave a Reply

You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <strong>