问题描述
- hash_map,unordered_map以string作为键,插入一千多万条数据后崩溃
-
代码:
#include "stdafx.h"
#include"iostream"
#include
#include
#include
#include
#include
using namespace std;typedef hash_map myMap;
void CharsAllSequen(myMap &resultMap, string &str, unsigned int n, double defaultVal);
void Clear0ForStrQuen(myMap &resultMap);int main()
{myMap totalMap; myMap resultMap; string str; CharsAllSequen(resultMap, str, 6, 0.00323223); stringstream ss; for (int i = 0; i < 3000; i++) { myMap::iterator iter; for (iter = resultMap.begin(); iter != resultMap.end(); ++iter) { string strTemp = iter->first; ss.str(""); ss.clear(); ss << "_"; ss << i; strTemp += ss.str(); totalMap.insert(myMap::value_type(strTemp, iter->second + i / 3001.0)); } } return 0;
}
void Clear0ForStrQuen(myMap &resultMap)
{
myMap::iterator iter = resultMap.begin();
while (iter!=resultMap.end())
{
iter->second = 0;
++iter;
}
}
//获取特定长度,由0,1,2,3四个字符组成的字符串,比如010203,作为key,值设置为defaultVal
void CharsAllSequen(myMap &resultMap, string &str, unsigned int n, double defaultVal)
{
if (n==0)
{
if (str.size()>0)
{
resultMap.insert(myMap::value_type(str, defaultVal));
}
else
{
return;
}} else { for (int i = 0; i < 4; i++) { stringstream ss; ss << i; string temp = ss.str(); str.append(temp); CharsAllSequen(resultMap, str, n - 1, defaultVal); str = str.substr(0, str.size() - 1); } }
}
解决方案
引发这个错误的原因是内存不足,在诊断工具里看下内存吧
解决方案二:
map hash_map unordered_map map, hash_map,unordered_map
map, hash_map,unordered_map
解决方案三:
http://bbs.csdn.net/topics/330220139