我写了这段代码:
运行时,该行出现错误
cout<<it2->first;
test3.exe 中 0x00411edd 处未处理的异常:0xC0000005:访问冲突读取位置 0x00000004。”
我有 Visual Studio Express 2008 和 Boost 1_47_0;
这是我的完整代码:
#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---------------------------