Table Of Contents

Previous topic

8.5. Structure and format of the image and shapes files

Next topic

8.7. Introduction to make

Download

8.6. STL: the (C++) standard template library

C++ itself has very few tools for managing sequences of characters, inputs/outputs and collections.

The STL library provides C++ with a standardized answer using the C++ proper mechanisms as:

  • the object approach and capacity for abstraction with the operators
  • use of templates
  • operators overdefinition

The effective normalization of STL library make it a whole part of C++, and we will always use STL library for the management of strings, IO and collections.

In particular, the overwhelming use of this library has proven its reliability and its level of optimization, both in the use of memory or in performance.

Below we will describe only a few of services offered by STL.

8.6.1. Strings

The std::string class manages sequences of characters.

#include <string>

int main (int argc, char **argv)
{
  std::string text = "abc defg";

  std::string words[] = { "aaa", "bbb", "ccc", "ddd", "eee" };

  text += words[0];

  return (0);
}

8.6.1.1. Main operations with strings

  • Assigning strings

    std::string s = "abcd";
    std::string t = s;
    
    t += "abcd";
    
  • String length

    std::string s = "abcd";
    
    int size = s.size ();
    
  • Find content in string

    std::string s = "abcd";
    
    int pos = s.find ("bc");
    
  • Getting C string equivalent for C functions

    std::string s = "abcd";
    
    if (!strcmp (s.c_str (), "abcd")) ...
    ...
    
  • Use with std::getline() fonction

    std::ifstream f;
    std::string s;
    
    std::getline (f, s);
    ...
    

8.6.2. Vectors

The std::vector class provides a linear sequence container for any type of object (which can be copied).

  • base types:

    #include <vector>
    
    int main (int argc, char **argv)
    {
      std::vector<int> v;
    
      for (int i = 0; i < 10; i++) {
        v.push_back (i);
      }
      return (0);
    }
    
  • user objects:

    #include <vector>
    
    class A
    {
    public:
      A (int value) : m_value(value) {}
    
    private:
      int m_value;
    };
    
    int main (int argc, char **argv)
    {
      std::vector<A> v;
    
      for (int i = 0; i < 10; i++) {
        v.push_back (A(i));
      }
      return (0);
    }
    
  • references or pointers to user objects:

    #include <vector>
    
    class A
    {
    public:
      A (int value) : m_value(value) {}
    
    private:
      int m_value;
    };
    
    int main (int argc, char **argv)
    {
      std::vector<A*> v;
    
      for (int i = 0; i < 10; i++) {
        v.push_back (new A(i));
      }
      // somehow use v...
    
      // clean-up: we need to call delete on each 
      // of the pointers, otherwise the memory will
      // be leaked...
      for (std::vector<A*>::iterator
             itr = v.begin(),
             iend= v.end();
           itr != iend;
           ++itr) {
        delete *itr;
        // also put the pointer to NULL. useful for debugging
        // and to prevent inadvertant double deletes...
        *itr = 0;
      }
    
      return (0);
    }
    
  • vectors of vectors:

    #include <vector>
    
    int main (int argc, char **argv)
    {
      std::vector< std::vector< int > > v;
    
      for (int x = 0; x < 10; x++) {
    
        std::vector< int >  column;
        for (int y = 0; y < 10; y++) {
          column.push_back (y*2);
        }
        v.push_back (column);
      }
      return (0);
    

8.6.2.1. Main operations acting on a vector

  • Adding an element at the end of a vector

    v.push_back (12);
    
  • Removing the last element from a vector

    v.pop_back ();
    
  • Removing all elements from a vector

    v.clear ();
    
  • Getting the first et the last element from a vector

    int first = v.front ();
    int last  = v.back ();
    
  • Getting an element by its position

    int i = v[2];
    
  • Setting an element localized by its position

    v.at(2) = 3;
    

8.6.2.2. Iterations in a vector

One can iterate through the elements of a vector using a category of iterator:

std::vector<T>::iterator

std::vector<T>::const_iterator

std::vector<T>::reverse_iterator

std::vector<T>::const_reverse_iterator

#include <iostream>

#include <vector>

typedef std::vector<int> int_vector;

int main (int argc, char **argv)
{
  int_vector v;

  for (int i = 0; i < 10; i++) {
    v.push_back (i);
  }

  int_vector::iterator it;

  for (it = v.begin (); it != v.end (); ++it) {

    int i = *it;

    std::cout << "i = " << i << std::endl;
  }

  return (0);
}

8.6.2.3. Some operations with iterators on vectors

  • Removes an element from a vector

    std::vector<int> v;
    std::vector<int>::iterator it;
    
    it = ...;
    
    v.erase (it);
    
  • Insert an element into a vector

    std::vector<int> v;
    std::vector<int>::iterator it;
    
    it = v.begin ();
    
    // Insert before the element pointed by it
    v.insert (it, 24);
    

8.6.3. Lists

Lists given by list class almost act as vectors, but offer an implementation based on doubly-linked lists providing efficient inserts and removes.

#include <list>
#include <iostream>

typedef std::list<int> int_list;

int main (int argc, char **argv)
{
  int_list lst;

  for (int i = 0; i < 10; i++) {

    lst.push_front (i);
    lst.push_back (i);
  }

  int_list::iterator it;

  for (it = lst.begin (); it != lst.end (); ++it) {

    int i = *it;

    std::cout << "i = " << i << std::endl;
  }

  return 0;
} 

8.6.4. Maps

The map class provides an indexed collection.

#include <iostream>

#include <map>
#include <string>

typedef std::map<int,std::string> dictionary;

int main (int argc, char **argv)
{
  dictionary d;

  std::string words[] = { "aaa", "bbb", "ccc", "ddd", "eee" };

  for (int i = 0; i < sizeof (words) / sizeof (std::string); i++) {

    d[i] = words[i];
  }

  dictionary::iterator it;

  for (it = d.begin (); it != d.end (); ++it) {

    int key = (*it).first;

    std::string word = (*it).second;

    std::cout << "word = " << word << std::endl;
  }

  return 0;
}

8.6.5. Some references