C ++ unordered_map错误 [英] c++ unordered_map erro

查看:156
本文介绍了C ++ unordered_map错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写此代码.在运行时此行"cout< it2-> first;"中出现一个错误:" test3.exe中0x00411edd的未处理异常:0xC0000005:访问冲突读取位置0x00000004.
我有Visual Studio Express 2008并提升了1_47_0;
这是我的代码.请帮帮我!

i write this code.in run time one error is occur in this line"cout<<it2->first;":"Unhandled exception at 0x00411edd in test3.exe: 0xC0000005: Access violation reading location 0x00000004."
i have visual studio express 2008 and boost 1_47_0;
this is my code.plz help me!

    #include "stdafx.h"
    #include <iostream>
    #include <boost/unordered_map.hpp>

using namespace std;

typedef boost::unordered_map<int,int > MAP;
        MAP map2;
        boost::unordered_map<int,int>::iterator it2;

void gen_random(char *s  ,char *p,int*  r,const int len);
void inline insert2(int i_key,int i_value);
void print();
//-----------main------------------------------------
void main()
{
    char* s_key=new char[8];
    char* s_value=new char[8];
    int i_value=0, size_random=8;
    for(int i=0;i<12;i++)
    {
        gen_random(s_key,s_value,&i_value,size_random);
         insert2(i_value,i_value);
    }

        print();
    int a;
    std::cin>>a;
}
//-------------end main--------------------------------

//--------my function ---------------------------
//-------random--------------
void gen_random(char* s,char* p,int *r, const int len) {


    static const char alphanum[] =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz";
     (*r)=rand();

    for (int i = 0; i < len; ++i) {
        s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
         p[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
    }

    s[len] = 0;
    p[len] = 0;
}
//-----end random------------

    void inline insert2(int i_key,int i_value)
    {
        map2.insert(MAP::value_type(i_key,i_value));
    }
    void print()
    {

    for(it2=map2.begin();it2 != map2.end();++it2 );
     {


         cout<<it2->first;

     }

    }
//--------end function---------------------------

推荐答案

这是一个问题...
您正在写这些字符串的末尾.

Here''s one problem...
You''re writing past the end of these strings.

s[len] = 0;
p[len] = 0;


我忘了在for的末尾删除分号.当我删除分号时,问题出在gon
i forgot remove semicolon in the end of for.when i remove that.the problem is gon


这篇关于C ++ unordered_map错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆