unordered_map:
一些漂亮的代码(简化变量名):
从这里http://en.cppreference.com/w/cpp/container/unordered_map/operator_at
std::unordered_map<char, int> mu1 {{'a', 27}, {'b', 3}, {'c', 1}};
mu1['b'] = 42; // update an existing value
mu1['x'] = 9; // insert a new value
for (const auto &pair: mu1) {
std::cout << pair.first << ": " << pair.second << '\n';
}
// count the number of occurrences of each word
std::unordered_map<std::string, int> mu2;
for (const auto &w : { "this", "sentence", "is", "not", "a", "sentence", "this", "sentence", "is", "a", "hoax"}) {
++mu2[w]; // the first call to operator[] initialized the counter with zero
}
for (const auto &pair: mu2) {
std::cout << pair.second << " occurrences of word '" << pair.first << "'\n";
}
你应该提供编译例子。否则,我们无法知道这些拼写错误是否确实是错误的。此外,你应该指定你的代码不工作的“如何”。 – mfontanini 2012-03-28 02:27:18