Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Version 5 (modified by dafrick, 16 years ago) (diff)

HowTo: STL

TracNav(TracNav/TOC_Development)?

Overview

STL stands for Standard Template Library. The STL implements several useful containers. This site provides a short overview of the most important containers.

vector Like an array, every element has an index Fast to access elements by position
list Elements are connected in a list, strictly ordered Fast if you just want to store elements
set Comparable with a search-tree Fast if you have to decide if an element is within the set or not
map Every element has a key Fast if you have to find elements identified by a key

Reference

For more information have a look at the reference. Strongly recommended.

Usage

vector

#include <vector>

std::vector<type> myVector;

type value1, value2, value3;
myVector.push_back(value1);
myVector.push_back(value2);
myVector.push_back(value3);

myVector[0]; // returns value1
myVector[2]; // returns value3

myVector.size(); // returns 3

list

#include <list>

std::list<type> myList;

type value1, value2, value3;
myList.push_back(value1);
myList.push_back(value2);
myList.push_back(value3);


// Iterate through all elements:
for (std::list<type>::iterator it = myList.begin(); it != myList.end(); ++it)
{
    std::cout << (*it) << std::endl;
}

// Output:
// value1
// value2
// value3


// Insert a new value at the beginning:
type value4;
myList.insert(myList.begin(), value4);

set

#include <set>

std::set<type> mySet;

type value1, value2, value3;
mySet.insert(value1); //
mySet.insert(value2); // Note: set has no order of the elements
mySet.insert(value3); //


// Tell if value2 is in the set:
if (mySet.find(value2) != mySet.end())
{
    set::cout << value2 << " is in the set!" << std::endl;
}


// Iterate through all elements:
for (std::set<type>::iterator it = mySet.begin(); it != mySet.end(); ++it)
{
    std::cout << (*it) << std::endl;
}

// Output (set sorts elements from smaller to greater):
// value1
// value2
// value3

map

#include <map>

std::map<std::string, type> myMap; // Note: map has two template arguments, key and type

type value1, value2, value3;
myMap["one"] = value1;
myMap["two"] = value2;
myMap["three"] = value3;


// Tell if an element with key "two" is in the map:
std::map<std::string, type>::iterator it = myMap.find("two");
if (it != myMap.end())
{
    set::cout << "An element with key 'two' is in the set!" << std::endl;
    std::cout << "The element is " << it->second << std::endl;
}


// Iterate through all elements:
for (std::map<std::string, type>::iterator it = myMap.begin(); it != myMap.end(); ++it)
{
    std::cout << it->first << ": " << it->second << std::endl;
}

// Output (map sorts keys from smaller to greater):
// one: value1
// three: value3
// two: value2


// Attention: operator[] adds an entry with defaultvalue:
if (myMap["four"] != myMap.end())
{
    std::cout << "An element with key 'four' is in the set!" << std::endl;
}
// As we know, 'four' is NOT in the map. But after our call to myMap["four"] there
// is now an empty entry for "four".
//
// If we iterate again through all elements, we get:
//
// Output (map sorts keys from smaller to greater):
// four: 
// one: value1
// three: value3
// two: value2
//
// Because this behaviour is usually unwanted, use find("four") like in the example above.