例题:
1 #include<iostream>
2
3 int main() 4 { 5 cout << "HelloWorel!" ; 6 return 0; 7 }
1 #include <iostream>
2 using namespace std; 3 int main() //c++ programs start by executing the function main
4 { 5 cout << "HelloWorld!\n"<< endl; //output"HelloWorld!"
6 return 0; 7 }
编译经过。ios
疑问:c++
书上的例题用VC++编译函数
我在Dev-c++编译同样报错spa
using namespace std;干吗用的?为毛跟书上的列题不同?3d
查查资料后了解得知:
#include<iostream.h> 非标准输入输出流,这个写法是之前C语言的写法,上个世纪八九十年代的书中通常采用这种写法,如今已经不适用了。
iostream.h时代没有名词空间,即全部库函数包括头文件iostream.h都声明在全局域。为了体现结构层次,避免名字定义冲突,code
c++标准委员会特别引入了“名字空间的定义”,即namespace。引入了名词空间这一律念,并把全部库函数声明由全局域改到了名词空间std。
因此,新的标准是:blog
1 #include <iostream> //标准输入输出流
2 using namespace std;
(由于iostream声明在std中,故而要加上这句,除非你不用库函数,不然错误); 编译器
不少编译器都同时支持这两种头文件形式,更好的固然是标准头文件。至于为何不废除非标准头文件,大概是为了兼容之前的代码吧。string
还有一点在标准c++中,全部库函数都没有.h后缀了,若是是c语言的库函数,则去掉后缀,并在开头加上一个c。
io
以下:
#include cstdio //stdio.h
#include cstring //string.h
使用<iostream>时,引入std::有如下方法:
1 using namespace std; 3 cout<<"Hello c++!"<<endl; 4 5 using std::cout; 6 cout<<"Hello c++!"; 7 8 std::cout<<"Hello c++!";