第三章 标准库类型
3.1命名空间的using声明
通常,头文件中应该只定义确定必要的东西。
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main(){
//some codes.....
}
C++标准程序库中的所有标识符都被定义于一个名为std的namespace中。
所以,比上面的声明方式更方便的就是使用using namespace std;
例如:
#include<iostream>
using namespace std;
这样命名空间std内定义的所有标识符都有效!!不必一个一个去写了!!
习题3.1
//使用适当的using声明,而不用std前缀,计算一给定数的给定幂的结果
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main(){
int val,pow,res=1;
cout<<"enter two numbers"<<endl;
cin>>val>>pow;
for(int cnt=1;cnt<=pow;cnt++)
res=res*val;
cout<<"The result of "<<val<<"^"<<pow<<" is "<<res<<endl;
return 0;
}