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