Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changes between Initial Version and Version 1 of code/howto/STL


Ignore:
Timestamp:
Oct 8, 2008, 2:17:39 AM (16 years ago)
Author:
landauf
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • code/howto/STL

    v1 v1  
     1= HowTo: STL =
     2[[TracNav(TracNav/TOC_Development)]]
     3[[TOC]]
     4
     5== Overview ==
     6|| '''vector''' || Like an array, every element has an index || Fast to access elements by position ||
     7|| '''list''' || Elements are connected in a list, strictly ordered || Fast if you just want to store elements ||
     8|| '''set''' || Comparable with a search-tree || Fast if you have to decide if an element is within the set or not ||
     9|| '''map''' || Every element has a key || Fast if you have to find elements identified by a key ||
     10
     11== Usage ==
     12=== vector ===
     13{{{
     14std::vector<type> myVector;
     15
     16type value1, value2, value3;
     17myVector.push_back(value1);
     18myVector.push_back(value2);
     19myVector.push_back(value3);
     20
     21myVector[0]; // returns value1
     22myVector[2]; // returns value3
     23
     24myVector.size(); // returns 3
     25}}}
     26
     27== list ==
     28{{{
     29std::list<type> myList;
     30
     31type value1, value2, value3;
     32myList.push_back(value1);
     33myList.push_back(value2);
     34myList.push_back(value3);
     35
     36
     37// Iterate through all elements:
     38for (std::list<type>::iterator = myList.begin(); it != myList.end(); ++it)
     39{
     40    std::cout << (*it) << std::endl;
     41}
     42
     43// Output:
     44// value1
     45// value2
     46// value3
     47
     48
     49// Insert a new value at the beginning:
     50type value4;
     51myList.insert(myList.begin(), value4);
     52}}}
     53
     54== set ==
     55{{{
     56std::set<type> mySet;
     57
     58type value1, value2, value3;
     59mySet.insert(value1); //
     60mySet.insert(value2); // Note: set has no order of the elements
     61mySet.insert(value3); //
     62
     63
     64// Tell if value2 is in the set:
     65if (mySet.find(value2) != mySet.end())
     66{
     67    set::cout << value2 << " is in the set!" << std::endl;
     68}
     69
     70
     71// Iterate through all elements:
     72for (std::set<type>::iterator = mySet.begin(); it != mySet.end(); ++it)
     73{
     74    std::cout << (*it) << std::endl;
     75}
     76
     77// Output (set sorts elements from smaller to greater):
     78// value1
     79// value2
     80// value3
     81}}}
     82
     83=== map ===
     84{{{
     85std::map<std::string, type> myMap; // Note: map has two template arguments, key and type
     86
     87type value1, value2, value3;
     88myMap["one"] = value1;
     89myMap["two"] = value2;
     90myMap["three"] = value3;
     91
     92
     93// Tell if an element with key "two" is in the map:
     94std::map<std::string, type>::iterator it = myMap.find("two");
     95if (it != myMap.end())
     96{
     97    set::cout << "An element with key 'two' is in the set!" << std::endl;
     98    std::cout << "The element is " << it->second << std::endl;
     99}
     100
     101
     102// Iterate through all elements:
     103for (std::map<std::string, type>::iterator = myMap.begin(); it != myMap.end(); ++it)
     104{
     105    std::cout << it->first << ": " << it->second << std::endl;
     106}
     107
     108// Output (map sorts keys from smaller to greater):
     109// one: value1
     110// three: value3
     111// two: value2
     112
     113
     114// Attention: operator[] adds an entry with defaultvalue:
     115if (myMap["four"] != myMap.end())
     116{
     117    std::cout << "An element with key 'four' is in the set!" << std::endl;
     118}
     119// As we know, 'four' is NOT in the map. But after our call to myMap["four"] there
     120// is now an empty entry for "four".
     121//
     122// If we iterate again through all elements, we get:
     123//
     124// Output (map sorts keys from smaller to greater):
     125// four:
     126// one: value1
     127// three: value3
     128// two: value2
     129//
     130// Because this behaviour is usually unwanted, use find("four") like in the example above.
     131}}}