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() {
37
37
{0 , " zero" },
38
38
{1 , " one" },
39
39
};
40
- assert (m[ 0 ] == " zero" );
41
- assert (m[ 1 ] == " one" );
40
+ assert (m. at ( 0 ) == " zero" );
41
+ assert (m. at ( 1 ) == " one" );
42
42
}
43
43
44
44
// # emplace
@@ -53,18 +53,22 @@ int main() {
53
53
assert ((m.emplace (0 , " zero" ).second ));
54
54
assert ((m.emplace (1 , " one" ).second ));
55
55
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" );
58
58
}
59
59
60
60
// # operator[]
61
61
//
62
62
// Get value from a given key.
63
63
//
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:
65
67
//
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
68
72
{
69
73
std::map<int ,std::string> m{
70
74
{0 , " zero" },
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments