@@ -37,7 +37,7 @@ std::string map_to_str(std::map<K,V> map) {
3737class RangeSwitch {
3838public:
3939 int method (int x) {
40- static std::map<int ,std::function<void (int &)>> m = {
40+ static const std::map<int ,std::function<void (int &)>> m{
4141 {0 , [&](int &x){
4242 x = -1 ;
4343 }},
@@ -51,8 +51,9 @@ class RangeSwitch {
5151 x = 5 ;
5252 }}
5353 };
54+ static const auto end = m.end ();
5455 auto it = m.upper_bound (x);
55- if (it == m. end () ) {
56+ if (it == end) {
5657 x = 7 ;
5758 } else {
5859 it->second (x);
@@ -95,7 +96,12 @@ int main() {
9596 /*
9697 # operator[]
9798
98- get value from a given key.
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
99105 */
100106 {
101107 std::map<int ,std::string> m{
@@ -110,7 +116,7 @@ int main() {
110116 assert (m[1 ] == " one2" );
111117
112118 // WARNING: if the key does not exist, it is inserted with a value with default constructor.
113- // This can be avoided by using `find` instead of `[]`.
119+ // This can be avoided by using `find` or `at` instead of `[]`.
114120 // Inserts `(2,"")` because `""` is the value for the default String constructor.
115121 // http://stackoverflow.com/questions/10124679/what-happens-if-i-read-a-maps-value-where-the-key-does-not-exist
116122 {
@@ -179,6 +185,25 @@ int main() {
179185 }
180186 }
181187
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+ */
197+ {
198+ std::map<int ,std::string> m{
199+ {0 , " zero" },
200+ {1 , " one" },
201+ };
202+ // Returns a reference, so we can modify it.
203+ m.at (1 ) = " one2" ;
204+ assert (m.at (1 ) == " one2" );
205+ }
206+
182207 /*
183208 # insert
184209
@@ -278,7 +303,7 @@ int main() {
278303 // # Range switch case.
279304 // http://stackoverflow.com/questions/9432226/how-do-i-select-a-range-of-values-in-a-switch-statement/42331563#42331563
280305 {
281- auto result = std::map<int , int >{
306+ const std::map<int , int > result {
282307 {-1 , -1 },
283308 { 0 , 0 },
284309 { 1 , 0 },
@@ -290,7 +315,7 @@ int main() {
290315 { 7 , 7 },
291316 };
292317 int x;
293- std::map<int ,std::function<void ()>> m = {
318+ const std::map<int ,std::function<void ()>> m{
294319 {0 , [&](){
295320 x = -1 ;
296321 }},
@@ -304,20 +329,21 @@ int main() {
304329 x = 5 ;
305330 }}
306331 };
332+ const auto end = m.end ();
307333 for (auto i = -1 ; i < 8 ; ++i) {
308334 auto it = m.upper_bound (i);
309- if (it == m. end () ) {
335+ if (it == end) {
310336 x = 7 ;
311337 } else {
312338 it->second ();
313339 }
314- assert (x == result[i] );
340+ assert (x == result. at (i) );
315341 }
316342
317343 RangeSwitch rangeSwitch;
318344 for (auto i = -1 ; i < 8 ; ++i) {
319345 x = rangeSwitch.method (i);
320- assert (x == result[i] );
346+ assert (x == result. at (i) );
321347 }
322348 }
323349}
0 commit comments