C++第三天_标准名称空间
才发现以前写的代码都太繁琐,有一些更简单的写法..也许看到前边的代码.很多程序员都会笑话我!没有办法,新手.
今天就学了标准名称空间,抱歉,由于和前边的代码几乎一样,所以没有写注释了.
- #include <iostream>
- int main()
- {
- using std::cout;
- using std::endl;
- cout <<"hello there.\n";
- cout <<"here is 5: " << 5 << "\n";
- cout <<"the manipulator endl";
- cout <<"writres a new line to the screen." <<endl;
- cout <<"here is a very big number:\t" << 7000 << endl;
- cout <<"here is the sum of 8 and 5:\t";
- cout <<8+5 <<endl;
- cout <<" a u:\t\t\t\t";
- cout <<(float) 5/8 <<endl;
- cout <<"This is a big number: \t\t";
- cout <<(double) 7000*7000 << endl;
- char response;
- std::cin >> response;
- return 0;
- }
我做了一个测试,将using写到大括号里外的效果都是一样的.
- #include <iostream>
- using std::cout;
- using std::endl;
- int main()
- {
- cout <<"hello there.\n";
- cout <<"here is 5: " << 5 << "\n";
- cout <<"the manipulator endl";
- cout <<"writres a new line to the screen." <<endl;
- cout <<"here is a very big number:\t" << 7000 << endl;
- cout <<"here is the sum of 8 and 5:\t";
- cout <<8+5 <<endl;
- cout <<" a u:\t\t\t\t";
- cout <<(float) 5/8 <<endl;
- cout <<"This is a big number: \t\t";
- cout <<(double) 7000*7000 << endl;
- char response;
- std::cin >> response;
- return 0;
- }
下边和上边的代码有一些地方不一样,但是一样可以编译,并且编译出来的效果是完全一样的.
下边一段代码是使用关键字namespace,效果依然一样
- #include <iostream>
- using namespace std;
- int main()
- {
- cout <<"hello there.\n";
- cout <<"here is 5: " << 5 << "\n";
- cout <<"the manipulator endl";
- cout <<"writres a new line to the screen." <<endl;
- cout <<"here is a very big number:\t" << 7000 << endl;
- cout <<"here is the sum of 8 and 5:\t";
- cout <<8+5 <<endl;
- cout <<" a u:\t\t\t\t";
- cout <<(float) 5/8 <<endl;
- cout <<"This is a big number: \t\t";
- cout <<(double) 7000*7000 << endl;
- char response;
- std::cin >> response;
- return 0;
- }
Leave a Reply