Skip to content

Commit 4d1df26

Browse files
committed
move most_vexing_parse out
1 parent c39ffb6 commit 4d1df26

File tree

2 files changed

+11
-61
lines changed

2 files changed

+11
-61
lines changed

cpp/map.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff 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"},

cpp/most_vexing_parse.cpp

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)