| 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 | {{{ |
| 14 | std::vector<type> myVector; |
| 15 | |
| 16 | type value1, value2, value3; |
| 17 | myVector.push_back(value1); |
| 18 | myVector.push_back(value2); |
| 19 | myVector.push_back(value3); |
| 20 | |
| 21 | myVector[0]; // returns value1 |
| 22 | myVector[2]; // returns value3 |
| 23 | |
| 24 | myVector.size(); // returns 3 |
| 25 | }}} |
| 26 | |
| 27 | == list == |
| 28 | {{{ |
| 29 | std::list<type> myList; |
| 30 | |
| 31 | type value1, value2, value3; |
| 32 | myList.push_back(value1); |
| 33 | myList.push_back(value2); |
| 34 | myList.push_back(value3); |
| 35 | |
| 36 | |
| 37 | // Iterate through all elements: |
| 38 | for (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: |
| 50 | type value4; |
| 51 | myList.insert(myList.begin(), value4); |
| 52 | }}} |
| 53 | |
| 54 | == set == |
| 55 | {{{ |
| 56 | std::set<type> mySet; |
| 57 | |
| 58 | type value1, value2, value3; |
| 59 | mySet.insert(value1); // |
| 60 | mySet.insert(value2); // Note: set has no order of the elements |
| 61 | mySet.insert(value3); // |
| 62 | |
| 63 | |
| 64 | // Tell if value2 is in the set: |
| 65 | if (mySet.find(value2) != mySet.end()) |
| 66 | { |
| 67 | set::cout << value2 << " is in the set!" << std::endl; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | // Iterate through all elements: |
| 72 | for (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 | {{{ |
| 85 | std::map<std::string, type> myMap; // Note: map has two template arguments, key and type |
| 86 | |
| 87 | type value1, value2, value3; |
| 88 | myMap["one"] = value1; |
| 89 | myMap["two"] = value2; |
| 90 | myMap["three"] = value3; |
| 91 | |
| 92 | |
| 93 | // Tell if an element with key "two" is in the map: |
| 94 | std::map<std::string, type>::iterator it = myMap.find("two"); |
| 95 | if (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: |
| 103 | for (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: |
| 115 | if (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 | }}} |