🔴 `[]` 悄悄插了一个进去
(本条路线统一用 g++ -std=c++17 -O0 -Wall 编译)注意 size() 在三处的变化:
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> m;
m["a"] = 1;
std::size_t before = m.size();
int v = m["missing"]; /* 🔴 [] 查不到会**当场插一个**进去 */
std::size_t after = m.size();
std::size_t cnt = m.count("also_missing"); /* count 不会插 */
std::size_t final_n = m.size();
std::cout << before << "/" << v << "/" << after << "/"
<< cnt << "/" << final_n << "\n";
return 0;
}
全部评论