This is an old revision of the document!


The STL library has 3 types of container classes:

  • sequence containers - container classes that maintain the ordering of elements in the container. Examples are vectors, deques, and lists.
  • associative containers - container classes that automatically sort their inputs when the inputs are inserted into the container. Eg. are a set, multiset, map or multimap.
  • container adapters - special predefined containers that are adapted to specific uses, such as a LIFO stack or a FIFO queue.

STL containers provide a mechanism for traversing their contents called an iterator. An iterator is best visualized as a pointer to an element in the list with a set of overloaded operators that provide extra functionality.

Overloaded iterator operators:

  • Operator* dereferencing an iterator gives the element that the item is pointing to
  • Operator++ advances the iterator to point to the next element
  • Operator== & != gives basic comparison for two iterators to see if they point to the same element.
  • Operator= assign to a new location

Each container has functions that return an iterator.

  • begin() returns an iterator at the beginning of the container
  • end() returns an iterator at the start of the container
  • cbegin() returns a const iterator at the start of the container
  • cend() returns a const iterator at the end of the container

Iterators are members of an stl container. For example to get an iterator for a specific type of list:

std::map<int, std::string>::const_iterator mapIt = myMap.cbegin(); 

Vector

An std::vector is a sequence container dynamic array that can grow to add to more elements.

Deque

An std::deque is double ended array that can grow from each end?

List

An std::list is a sequence container where each element contains a pointer to the next and previous element. You can't randomly access elements, you have to “walk the list”. But inserting elements is very fast if you know where to insert them.

Set

An std::set is an ordered list of unique elements that get automatically sorted as we insert them.

Multiset

An std::multiset is an ordered list of elements that can contain duplicate data which is automatically sorted as we insert them.

Map

An std::map is an associated array which is a map, symbol table or dictionary that has a collection of key value pairs such that a key only shows up once.

Data pairs must be inserted into a map, and while you do this they are automatically sorted. You can make pairs with the *std::make_pair(x,y)* helper function.

std::map<int, std::string> myMap;
myMap.insert(std::make_pair(1, "mango"));

For every new there must be a delete. Use the following valgrind command to profile for mem leaks1):

valgrind --tool=memcheck --leak-check=yes name_of_exec

There are lots of options for debugging. I make extensive use of DDD with ROS which helped a lot. gdbgui is an interesting web based front end, but i tried it for 5 mins and ran into issues with it debugging a simple program.


  • c.1550288960.txt.gz
  • Last modified: 2019/02/16 03:49
  • by paul