7.4.1 Using C++ standard library templates
The C++ standard library 'libstdc++' supplied with GCC provides a
wide range of generic container classes such as lists and queues, in
addition to generic algorithms such as sorting. These classes were
originally part of the Standard Template Library (STL), which was a
separate package, but are now included in the C++ standard library
itself.
The following program demonstrates the use of the template library by
creating a list of strings with the template list<string>
:
#include <list>
#include <string>
#include <iostream>
using namespace std;
int
main ()
{
list<string> list;
list.push_back("Hello");
list.push_back("World");
cout << "List size = " << list.size() << '\n';
return 0;
}
No special options are needed to use the template classes in the
standard library; the command-line options for compiling this program
are the same as before:
$ g++ -Wall string.cc
$ ./a.out
List size = 2
Note that the executables created by g++
using the C++ standard
library will be linked to the shared library 'libstdc++', which is
supplied as part of the default GCC installation. There are several
versions of this library--if you distribute executables using the C++
standard library you need to ensure that the recipient has a compatible
version of 'libstdc++', or link your program statically using the
command-line option -static
.