1- /*
2- # map
3-
4- http://www.cplusplus.com/reference/map/map/
5-
6- Also comes in an unordered version `unordered_map`.
7-
8- Ordered.
9-
10- Also comes in an multiple value input version `multimap`.
11-
12- Does not require a hash function. Usually implemented as a self balancing tree such as a rb tree.
13-
14- # hashmap
15-
16- There seems to be no explicit hashmap container, only a generic map interface,
17-
18- However unordered_map is likely to be hashmap based.
19-
20- A nonstandard `hash_map` already provided with gcc and msvc++.
21- It is placed in the `std::` namespace, but it is *not* ISO.
22- */
1+ // # map
2+ //
3+ // http://www.cplusplus.com/reference/map/map/
4+ //
5+ // Also comes in an unordered version `unordered_map`.
6+ //
7+ // Ordered.
8+ //
9+ // Also comes in an multiple value input version `multimap`.
10+ //
11+ // Does not require a hash function. Usually implemented as a self balancing tree such as a rb tree.
12+ //
13+ // # hashmap
14+ //
15+ // There seems to be no explicit hashmap container, only a generic map interface,
16+ //
17+ // However unordered_map is likely to be hashmap based.
18+ //
19+ // A nonstandard `hash_map` already provided with gcc and msvc++.
20+ // It is placed in the `std::` namespace, but it is *not* ISO.
2321
2422#include " common.hpp"
2523
@@ -32,40 +30,8 @@ std::string map_to_str(std::map<K,V> map) {
3230 return result.str ();
3331}
3432
35- // How to use the range pattern from a method.
36- // The key is to static initialize the lambda map.
37- class RangeSwitch {
38- public:
39- int method (int x) {
40- static const std::map<int ,std::function<void (int &)>> m{
41- {0 , [&](int &x){
42- x = -1 ;
43- }},
44- {2 , [&](int &x){
45- x = 0 ;
46- }},
47- {5 , [&](int &x){
48- x = 2 ;
49- }},
50- {7 , [&](int &x){
51- x = 5 ;
52- }}
53- };
54- static const auto end = m.end ();
55- auto it = m.upper_bound (x);
56- if (it == end) {
57- x = 7 ;
58- } else {
59- it->second (x);
60- }
61- return x;
62- }
63- };
64-
6533int main () {
66- /*
67- Create initialized map.
68- */
34+ // Initializer list constructor.
6935 {
7036 std::map<int ,std::string> m{
7137 {0 , " zero" },
@@ -75,15 +41,13 @@ int main() {
7541 assert (m[1 ] == " one" );
7642 }
7743
78- /*
79- # emplace
80-
81- Put a value pair into the map without creating the pair explicitly.
82-
83- Only inserts if not already present.
84-
85- Needs gcc 4.8: http://stackoverflow.com/questions/15812276/stdset-has-no-member-emplace
86- */
44+ // # emplace
45+ //
46+ // Put a value pair into the map without creating the pair explicitly.
47+ //
48+ // Only inserts if not already present.
49+ //
50+ // Needs gcc 4.8: http://stackoverflow.com/questions/15812276/stdset-has-no-member-emplace
8751 {
8852 std::map<int ,std::string> m;
8953 assert ((m.emplace (0 , " zero" ).second ));
@@ -93,16 +57,14 @@ int main() {
9357 assert (m[1 ] == " one" );
9458 }
9559
96- /*
97- # operator[]
98-
99- Get value from a given key.
100-
101- Create if not present, so avoid this if possible and prefer the more restrictive methods:
102-
103- - use at() or find () for fetching and updating
104- - emplace() for putting new values
105- */
60+ // # operator[]
61+ //
62+ // Get value from a given key.
63+ //
64+ // Create if not present, so avoid this if possible and prefer the more restrictive methods:
65+ //
66+ // - use at() or find () for fetching and updating
67+ // - emplace() for putting new values
10668 {
10769 std::map<int ,std::string> m{
10870 {0 , " zero" },
@@ -138,18 +100,14 @@ int main() {
138100 }
139101 }
140102
141- /*
142- # find
143-
144- # check if in map
145-
146- Similar to `std::set` find with respect to the keys:
147- returns an iterator pointing to the pair which has given key, not the value.
148-
149- If not found, returns `map::end()`
150-
151- This is preferable to `[]` since it does not insert non-existent elements.
152- */
103+ // # find
104+ //
105+ // Similar to `std::set` find with respect to the keys:
106+ // returns an iterator pointing to the pair which has given key, not the value.
107+ //
108+ // If not found, returns `map::end()`
109+ //
110+ // This is preferable to `[]` since it does not insert non-existent elements.
153111 {
154112 std::map<int ,std::string> m{
155113 {0 , " zero" },
@@ -163,15 +121,13 @@ int main() {
163121 assert (m.find (2 ) == m.end ());
164122 assert (m.size () == 2 );
165123
166- /*
167- Get default provided value if key not present
168-
169- TODO: any less verbose way than finding and check != end? Like:
170-
171- m.get(key, default)
172-
173- I know I can define a helper, but come on...
174- */
124+ // Get default provided value if key not present
125+ //
126+ // TODO: any less verbose way than finding and check != end? Like:
127+ //
128+ // m.get(key, default)
129+ //
130+ // I know I can define a helper, but come on...
175131 {
176132 std::map<int ,int > m{};
177133 int default_ = 42 ;
@@ -185,15 +141,13 @@ int main() {
185141 }
186142 }
187143
188- /*
189- # at()
190-
191- A convenient version of find() that returns the item directly.
192-
193- Throws if not present, so better when the key is supposed to be there.
194-
195- C++11.
196- */
144+ // # at()
145+ //
146+ // A convenient version of find() that returns the item directly.
147+ //
148+ // Throws if not present, so better when the key is supposed to be there.
149+ //
150+ // C++11.
197151 {
198152 std::map<int ,std::string> m{
199153 {0 , " zero" },
@@ -204,17 +158,15 @@ int main() {
204158 assert (m.at (1 ) == " one2" );
205159 }
206160
207- /*
208- # insert
209-
210- Insert pair into map.
211-
212- The return value is similar to that of a set insertion with respect to the key.
213-
214- Just use emplace instead, less verbose as it was added after perfect forwarding.
215-
216- http://stackoverflow.com/questions/17172080/insert-vs-emplace-vs-operator-in-c-map
217- */
161+ // # insert
162+ //
163+ // Insert pair into map.
164+ //
165+ // The return value is similar to that of a set insertion with respect to the key.
166+ //
167+ // Just use emplace instead, less verbose as it was added after perfect forwarding.
168+ //
169+ // http://stackoverflow.com/questions/17172080/insert-vs-emplace-vs-operator-in-c-map
218170 {
219171 std::map<int ,std::string> m;
220172 std::pair<std::map<int ,std::string>::iterator,bool > ret;
@@ -234,19 +186,17 @@ int main() {
234186 assert (ret.second == false );
235187 }
236188
237- /*
238- # iterate
239-
240- Map is ordered:
241- http://stackoverflow.com/questions/7648756/is-the-order-of-iterating-through-stdmap-known-and-guaranteed-by-the-standard
242-
243- It is iterated in key `<` order.
244-
245- So this basically requires implementations to use balanced
246- trees intead of hashmap.
247-
248- Iteration returns key value pairs.
249- */
189+ // # iterate
190+ //
191+ // Map is ordered:
192+ // http://stackoverflow.com/questions/7648756/is-the-order-of-iterating-through-stdmap-known-and-guaranteed-by-the-standard
193+ //
194+ // It is iterated in key `<` order.
195+ //
196+ // So this basically requires implementations to use balanced
197+ // trees intead of hashmap.
198+ //
199+ // Iteration returns key value pairs.
250200 {
251201 std::map<int ,std::string> m{
252202 {1 , " one" },
@@ -264,11 +214,9 @@ int main() {
264214 assert (map_to_str (m) == " 0:zero, 1:one, " );
265215 }
266216
267- /*
268- # erase
269-
270- Remove element from map.
271- */
217+ // # erase
218+ //
219+ // Remove element from map.
272220 {
273221 // key version. Returns number of elements removed (0 or 1).
274222 {
@@ -299,51 +247,4 @@ int main() {
299247 assert ((m == std::map<int ,std::string>{{1 , " one" }}));
300248 }
301249 }
302-
303- // # Range switch case.
304- // http://stackoverflow.com/questions/9432226/how-do-i-select-a-range-of-values-in-a-switch-statement/42331563#42331563
305- {
306- const std::map<int , int > result{
307- {-1 , -1 },
308- { 0 , 0 },
309- { 1 , 0 },
310- { 2 , 2 },
311- { 3 , 2 },
312- { 4 , 2 },
313- { 5 , 5 },
314- { 6 , 5 },
315- { 7 , 7 },
316- };
317- int x;
318- const std::map<int ,std::function<void ()>> m{
319- {0 , [&](){
320- x = -1 ;
321- }},
322- {2 , [&](){
323- x = 0 ;
324- }},
325- {5 , [&](){
326- x = 2 ;
327- }},
328- {7 , [&](){
329- x = 5 ;
330- }}
331- };
332- const auto end = m.end ();
333- for (auto i = -1 ; i < 8 ; ++i) {
334- auto it = m.upper_bound (i);
335- if (it == end) {
336- x = 7 ;
337- } else {
338- it->second ();
339- }
340- assert (x == result.at (i));
341- }
342-
343- RangeSwitch rangeSwitch;
344- for (auto i = -1 ; i < 8 ; ++i) {
345- x = rangeSwitch.method (i);
346- assert (x == result.at (i));
347- }
348- }
349250}
0 commit comments