Noeud:libstdc++ and the Standard Template Library, Noeud « Next »:, Noeud « Previous »:The GNU C Library, Noeud « Up »:Top



libstdc++ and the Standard Template Library

This chapter introduces libstdc++. Well, almost; libstdc++ incorporates many things, one of which is the GNU implementation of the Standard Template Library (STL). So instead we'll be looking at a small, concentrated part of libstdc++, in the form of the STL - justifiably so, given only one chapter. The Standard Template Library is very large and complex area of study; even reference books on it contain hundreds of pages. Therefore, this chapter looks at some of the more obvious uses of the STL, and detailed use is left for you to explore.

Why the STL? The STL is a large collection of useful programming utilities created to make programmers lives a lot easier. There are implementations of different containers that can hold data (such as lists, sets etc.), as well as generic algorithms that can be used with many of these containers. The STL is also standardised, meaning that wherever it is implemented, the interface and the results will be the same (unless the implementing parties didn't keep to the standard...). Also, the GNU project has worked hard at bringing it's implementation in accordance with the standard.

This chapter assumes that you already have knowledge of C++ and a fairly good understanding of object-oriented concepts. A good understanding of templates will also be useful. How the STL is Structured introduces the some of the basic components we'll be looking at, and how all these components work together. If you are new to STL, this is the place to start. We'll then look at practical use of containers (that store collections of objects) and iterators (used to traverse containers) in Containers and Iterators. We'll then look at Generic Algorithms and Function Objects and see how we can combine algorithms and function objects with containers to provide a powerful set of programming tools to work with. Strings shows us how STL provides an easy-to-use interface to strings, and how we can use STL strings with generic algorithms. A reference section is also provided, see STL Reference Section, giving a breakdown of all the commands used in this chapter as well as many more besides. Finally, Further Reading provides a list of books and links for further reading.

The source code examples in this chapter all invoke g++, the GNU C++ compiler. Full details about compilation are given in The GNU Compiler Collection, although the examples given throughout this section will be easy enough for you not to have to worry about reading ahead. For example, the compilation command for the vector1.cc source file (given in Vector), is:

g++ vector1.cc -o vector1

This means that you should type this into the command prompt in the directory where vector1.cc is located, hit <ENTER>, and it will compile the source file vector1.cc and produce a binary named vector1 (-o vector1 means name the output file, or binary, vector1); you'd run the binary by typing

$ ./vector1

at the command prompt in the directory where vector1 is located.