在我計劃的一部分,最有效的方法,我想更新的unordered_map
的給定值。這個問題可以簡化如下:在讀使用C++ 11 unordered_map /寫
#include <iostream>
#include <stdlib.h>
#include <unordered_map>
#include <string>
int main(int argc, char **argv) {
std::unordered_map <unsigned long, int> map;
unsigned long i = 1;
std::string s;
while (i != 0) {
std::cout << "Give me an unsigned long" << std::endl;
std::cin >> s;
i = strtoul(s.c_str(), NULL, 0);
if (map.find(i) == map.end()) map[i] = 1;
else if (map[i] < static_cast<unsigned long>(-1)) map[i] += 1;
}
}
(當然,我的程序不會存儲用戶的輸入數據,它只是爲了說明我的程序實際存儲在4個字母的字母表整數編碼的字符串。我需要的哈希值,而不是樹木,這樣做)。
正如你看到的,我需要檢查的一個關鍵的存在,並進行更新。由於我存儲了超過10億個整數,所以我想知道最有效的方法。
我看到this related question,但它並沒有提到值更新。
非常感謝大家。
注意,在代替鑄造,您可能需要使用'的std :: numeric_limits :: MAX()' –
@phresnel:非常真實的。我仍然不太熟悉C++ 11的限制。然而,它會計算'地圖[I]',然後得到它的類型(事我不想),或者是等同於'的std :: numeric_limits :: MAX()'? –
unamourdeswann