c++中的几个初级问题

来源:百度知道 编辑:UC知道 时间:2024/05/15 08:01:28
在c++中预处理命令如#include<iostream>,有时写成#include<iostream.h>,这个.h什么时候加,什么时候不加啊?
还有一个问题:using namespace std;到底有什么用?什么时候需要加?

#include<iostream>
是C++风格

你写成
#include<iostream>
using namespace std;

变相就等价于
#include<iostream.h> //这是C式风格

namespace 译成命名空间 你可以到C++书中的后续章节找一找
有的放在开始几章讲,有的却放在了最后才讲

最开始编程的时候,你可以采用
#include<iostream>
using namespace std;
以寻求容易理解

等到时间久了,能感觉到它的不足的时候,你就会理解为什么不用
using namespace std了.

Sample1:
#include<iostream>
using namespace std;
int main(){
cout<<"hello world!"<<endl;
return 0;
}

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

Sample3:
//C style
#include<iostream.h>
int main(void){
cout<<"hello world!"<<endl;
return 0;
}

当你写#include<iostream>时,要using namespace std;
当#include<iostream.h>时不要 using namesp