I have a friend at work who encouraged me to never assign a key/value pair into an empty map like the following:
int somefunc(map<int, int> somemap) {
somemap.clear();
somemap[12] = 42;
}
He said that since the somemap map variable was cleared, then somemap[12] is an invalid access. I reasoned that no C++ compiler, even when compiling in debug mode, would ever produce assembly that would unnecessarily try to access the somemap[12] on the assignment above. That it is always the case that the last line above would be compiled to the same assembly as this line:
somemap.insert(std::pair(12,42));
Is that true? Is there any reason to do assignment via insert vs. the earlier method? I prefer the earlier as it's shorter.