The Standard Library: Overview

Steven J. Zeil

Old Dominion University, Dept. of Computer Science

Table of Contents

1. std/STL Containers
2. The std Containers
2.1. Sequential and Associative
2.2. Consistent Interfaces
2.3. A Matter of Style

A large project builds upon libraries of ADTs built for that specific project, or that have been collected by the company for related projects (a.k.a. domain-specific ADTs). These in turn are generally built upon the library of ADTs provided by all C++ compilers. This standard C++ library is called std.

1. std/STL Containers

The key components of std are

  • I/O library: e.g., streams

  • Utility ADTs: e.g., string

  • Containers (ADTs that hold one or more values of some other ADT): vector,set, …

  • Iterators: "positions" within a container

  • Algorithms: common programming "patterns"

You are already familiar with many parts of std. You presumably were already familiar with the I/O portion. We have already looked at some utilities, and some portions of the Iterators and Algorithms portion. Now we want to do a quick overview of the containers portion, prior to beginning to look at some of the specific containers in detail.

You may or may not have heard of the STL library before. When the C++ standards committee was considering what should be included in the standard library, std, the early drafts included what we now recognize as the I/O and utility sections. A group of researchers from HP then proposed addition of their STL library to std. STL consisted of a set of containers, iterators, and function templates (algorithms). STL stands for Standard Template Library, which may have been a bit presumptuous because it certainly was not part of the standard at the time. The C++ standards committee eventually accepted the STL, with some revisions, as part of std.

2. The std Containers

We call an ADT a container if its main purpose is to provide a collection of items of some other, simpler type.

2.1. Sequential and Associative

Collections come in two flavors

sequential:

- values are accessed in an order determined by their insertion.

The std sequential containers are vector, deque, list, stack, queue, and priority queue.

associative:

- allow "random" access

The std associative containers are set, map, multi-set, and multi-map.

2.2. Consistent Interfaces

One goal of the std library is to provide consistent interfaces to many different containers.

  • Many operations are common to multiple ADTS.

  • These are always given the same name and formal parameter lists.

Examples:

  • If a container allows you to add and remove elements from its end, those operations are always called push_back and pop_back.

    myContainer.pop_back (); // discard last element from container
    myContainer.push_back (x); // add x to end of container
    

    Similarly, if a container allows you to add and remove elements from its front, those operations are always called push_front and pop_front.

  • Operations to add and remove elements at arbitrary positions are always called insert and erase. These use iterators to specify the position:

    myContainer.insert (x, position); // insert x at the indicated
                                      //     position, shifting other
                                      //     elements out of the way
    myContainer.erase (position);     // discard the element at that position
    myContainer.erase (start, stop);  // discard all elements at positions
                                      //     start up to (but not including)
                                      //     stop.
    
  • You can test to see if a container is empty with empty(). You can ask how many items are in the container with size().

    if (myContainer.empty())
      {
         assert (myContainer.size() == 0);
      }
    
  • If a container provides iterators, these are called iterator and const_iterator. The beginning position in the container is given by begin(). The position just after the last element is given by end(). When the container is empty, begin()==end().

2.3. A Matter of Style

Because of this consistency in the interfaces, learning to use the std containers is more a matter of learning the std style than of learning 10 different ADT interfaces.

You can find a handy summary of the container interfaces in my STL containers reference sheets. You can also find some very good overviews and documentation from SGI.


In the Forum:

(no threads at this time)