Next: , Previous: C++ compilation options, Up: Compiling a C++ program


7.3 Using the C++ standard library

An implementation of the C++ standard library is provided as a part of GCC. The following program uses the standard library string class to reimplement the Hello World program:

     #include <string>
     #include <iostream>
     
     using namespace std;
     
     int
     main ()
     {
       string s1 = "Hello,";
       string s2 = "World!";
       cout << s1 + " " + s2 << '\n';
       return 0;
     }

The program can be compiled and run using the same commands as above:

     $ g++ -Wall hellostr.cc
     $ ./a.out
     Hello, World!

Note that in accordance with the C++ standard, the header files for the C++ library itself do not use a file extension. The classes in the library are also defined in the std namespace, so the directive using namespace std is needed to access them, unless the prefix std:: is used throughout (as in the previous section).