File tree Expand file tree Collapse file tree 2 files changed +11
-61
lines changed Expand file tree Collapse file tree 2 files changed +11
-61
lines changed Original file line number Diff line number Diff line change @@ -37,8 +37,8 @@ int main() {
3737 {0 , " zero" },
3838 {1 , " one" },
3939 };
40- assert (m[ 0 ] == " zero" );
41- assert (m[ 1 ] == " one" );
40+ assert (m. at ( 0 ) == " zero" );
41+ assert (m. at ( 1 ) == " one" );
4242 }
4343
4444 // # emplace
@@ -53,18 +53,22 @@ int main() {
5353 assert ((m.emplace (0 , " zero" ).second ));
5454 assert ((m.emplace (1 , " one" ).second ));
5555 assert (!(m.emplace (1 , " one2" ).second ));
56- assert (m[ 0 ] == " zero" );
57- assert (m[ 1 ] == " one" );
56+ assert (m. at ( 0 ) == " zero" );
57+ assert (m. at ( 1 ) == " one" );
5858 }
5959
6060 // # operator[]
6161 //
6262 // Get value from a given key.
6363 //
64- // Create if not present, so avoid this if possible and prefer the more restrictive methods:
64+ // Creates if not present, so be very careful if that's not what you want!
65+ //
66+ // Use:
6567 //
66- // - use at() or find () for fetching and updating
67- // - emplace() for putting new values
68+ // - this to "add new or update existing" or "create default value and return it"
69+ // - at() to find when you are sure it is there
70+ // - find() to find when you are not sure it is there
71+ // - emplace() for putting new values when you are sure they are not there
6872 {
6973 std::map<int ,std::string> m{
7074 {0 , " zero" },
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments