Why does setting an element of a map to its size increments the size *before* assigning it?
问题 This is a common pattern I use to index tokens as they come in: check if the token is in a map, and if not, add it to the map, assigning the map's size. When doing this in C++, it unexpectedly increments the map's size before the assignment is made: #include <cstdio> #include <map> using namespace std; int main() { map<char, int> m; printf("Size before adding: %d\n", m.size()); m['A'] = m.size(); printf("Size after adding: %d\n", m.size()); printf("What was added: %d\n", m['A']); return 0; }