meng_521
浊世老先生
采纳率100%
2016-03-30 07:12 阅读 4.1k

hash_map,unordered_map以string作为键,插入一千多万条数据后崩溃

1

代码:
#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);
    }
}

}

图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    qq_34509904 qq_34509904 2016-04-01 13:08

    引发这个错误的原因是内存不足,在诊断工具里看下内存吧

    点赞 评论 复制链接分享
  • qq_34509904 qq_34509904 2016-04-01 13:09
    点赞 评论 复制链接分享

相关推荐