The Standard Template Library (STL)

A library that makes heavy use of
templates, operator overloading,
and inheritance to provide easy-to-use
and efficiently implemented classes.

Similar to the Collections API in java.util


Four items to know about in the STL:

  1. containers
  2. iterators
  3. function objects
  4. algorithms

Containers

  1. vector
  2. list
  3. stack
  4. queue
  5. priority_queue
  6. deque
  7. set
  8. multiset
  9. map
  10. multimap
  11. bitset

Diagram by Jak Kirman


Iterators

Much like pointers.
They allow the identification or specification
of a specific item within a container.
They allow the specification of ranges
within a container.

iterator concepts:

  1. input iterator
  2. output iterator
  3. forward iterator
  4. bidirectional iterator
  5. random access iterator

input iterator


output iterator


forward iterator


bidirectional iterator

Has all aspects of forward iterator:

Plus:


random access iterator

Has all aspects of bidirectional iterator:

Plus:


All iterators have the common methods:

begin(), which returns an iterator
referring to the first item in the container

end(), which returns an iterator
referring to one past the end of the container


Declaring an iterator
(as appropriate to the container):

  vector<double>::iterator ptemps;

The Vector container

a sequence container

much like a template class implementation
of a dynamically-sized array


A little program to play with a vector:

#include <iostream>
#include <vector>
#include <iterator>
using namespace std;

int main(int argc, char *argv[]) {

  vector<int> scores(3);
  vector<int>::iterator pvector;

  scores.push_back(13);
  scores.push_back(-2);
  scores.push_back(60);
  scores[2] = 100;

  cout << "# elements in vector is " 
       << scores.size() << endl;
  cout << "Contents of vector:" << endl;
  for (pvector = scores.begin(); 
           pvector != scores.end(); pvector++) {
    cout << *pvector << " ";
  }
  return 0;
}

This program prints output of:

# elements in vector is 6
Contents of vector:
0 0 100 13 -2 60 

The Deque container
(a double-ended queue)

a sequence container

much like the vector


A little program to play with a deque:

#include <iostream>
#include <deque>
#include <iterator>
using namespace std;

int main(int argc, char *argv[]) {
  deque<int> D;
  deque<int>::iterator pd;

  D.push_back(1);
  D.push_back(2);
  D.push_front(3);
  D.push_front(4);
  int value = D.front();
  D.pop_front();

  cout << "# elements in deque is " 
       << D.size() << endl;
  cout << "Contents of deque:" << endl;
  for (pd = D.begin(); pd != D.end(); pd++) {
    cout << *pd << " ";
  }
  cout << "\nfront value was " << value << endl;
  return 0;
}

This program prints output of:

# elements in deque is 3
Contents of deque:
3 1 2 
front value was 4

Associative containers
(set, multiset, map, multimap)

for set and multiset, include

  #include <set>

for map and multimap, include

  #include <map>

Members of an associative container are pairs.
Each pair associates a key with a value.

Lookups use the key.


set

associative, reversible, sorted, with unique keys

The key type and the value type
are the same, and values are equivalent to keys.

set<int> my_set;  // declaration

And, if you want to define your own
comparison function:

set<int, less<int> > my_set;

A little program to play with a set:

#include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include <iterator>
using namespace std;

int main(int argc, char *argv[]) {

  string s1[4] = {"cat", "on", "tin", "roof"};
  string s2[4] = {"time", "on", "my", "hands"};
  set<string> S1(s1, s1+4);
  set<string> S2(s2, s2+4);

  ostream_iterator out(cout, " ");
  cout << "S1:  ";
  copy(S1.begin(), S1.end(), out);
  cout << endl;
  cout << "S2:  ";
  copy(S2.begin(), S2.end(), out);
  cout << endl;

  cout << "Union of S1 and S2:  ";
  set_union(S1.begin(), S1.end(),
            S2.begin(), S2.end(), out);
  cout << endl;

  cout << "Intersection of S1 and S2:  ";
  set_intersection(S1.begin(), S1.end(), 
                   S2.begin(), S2.end(), out);
  cout << endl;
  return 0;
}

This program prints output of:

S1:  cat on roof tin 
S2:  hands my on time 
Union of S1 and S2:  cat hands my on roof time tin 
Intersection of S1 and S2:  on

multiset
The key type and the value type ore the same, and values are equivalent to keys.
Duplicates may exist.


map


multimap


multimap<int, string> M;  // declaration

Algorithms

Used as appropriate on containers.

All use templates.

Most need iterators.

Some need function objects (functors).

  #include <algorithm>

Some of the algorithms:


No time to talk about:

Function objects (functors).

The auto_ptr template.

The Boost libraries.
These are extensions of the STL.


Boost C++ libraries


Copyright © Karen Miller, 2007