unordered_map c++错误
我编写这段代码:
运行时,该行发生错误
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---------------------------
I write this code:
On running, an error occurs in the 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 full code:
#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---------------------------
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在尝试为数组边界之外的
s[8]
和p[8]
赋值。它们被声明为char[8]
,因此只能从0
到7
进行索引。You are trying to assign values to
s[8]
andp[8]
which are outside the array bounds. They are declared aschar[8]
so they can only be indexed from0
to7
.该行:
不应以分号结尾。循环运行,但不会进入其后面的代码块。循环完成后,
it2
等于map2.end()
,这就是当您尝试使用它时会遇到访问冲突的原因。如果您使用较小范围的变量,则可以避免此问题。例如,这会导致编译器错误(假设
it2
不再是全局的):The line:
Should not end in a semicolon. The loop runs, but does not go into the block of code following it. When the loop completes,
it2
is equal tomap2.end()
which is why you get an access violation when you try to use it.This problem could have been avoided if you used a smaller scoped variable. For example, this would get you a compiler error (assuming
it2
was no longer a global):