7.2使用C++标准库

一种实现方法是将C++标准库作为GCC的一部分。下面的程序使用标准库string类重新实现Hello World程序:

include <string>

include <iostream>

using namespace std;

Int

main ()

{

string s1 = "Hello,";

string s2 = "World!";

cout << s1 + " " + s2 << endl;

return 0;

}

这个程序可以使用上面的命令编译和运行程序:

$ g++ -Wall hellostr.cc

$ ./a.out

Hello, World!

注意为了配合C++标准库,C++库自己的头文件不能使用文件扩展后缀。库中的类也定义在std命名空间中,因此using namespace std的代码可以访问他们,除非全篇使用std::前缀。