0xC0000005:访问冲突执行位置 0x00000000。当尝试访问 multimap 的插入功能时
0xC0000005: Access violation executing location 0x00000000. When trying to access the insert function of multimap
我正在为 USACO 培训页面中的一个问题编写代码,当我尝试调试我的代码时:
#include <iostream>
#include<fstream>
#include<string>
#include<map>
#include<math.h>
using namespace std;
int translate(string &s) {
int n = s.length();
int result = 0;
for (int i = 0; i < s.length(); i++) {
int n1 = 0;
if (s.at(i) == 'Q' || s.at(i) == 'Z') {
return -1;
}
switch (s.at(i)) {
case 'A':
case 'B':
case'C':
result = result + 2 * pow(10, n-1);
n--;
break;
case 'D':
case 'E':
case'F':
result = result + 3 * pow(10, n - 1);
n--;
break;
case 'G':
case 'H':
case 'I':
result = result + 4 * pow(10, n - 1);
n--;
break;
case 'J':
case 'K':
case 'L':
result = result + 5 * pow(10, n - 1);
n--;
break;
case 'M':
case 'N':
case 'O':
result = result + 6 * pow(10, n - 1);
n--;
break;
case 'P':
case 'R':
case 'S':
result = result + 7 * pow(10, n - 1);
n--;
break;
case 'T':
case 'U':
case 'V':
result = result + 8 * pow(10, n - 1);
n--;
break;
case 'W':
case 'X':
case 'Y':
result = result + 9 * pow(10, n - 1);
n--;
break;
}
}
return result;
}
bool mycompare(int n, int m) {
string a, b;
a = to_string(n);
b = to_string(m);
if (a < b) {
return true;
}
else return false;
}
int main() {
bool(*ptr)(int, int);
typedef multimap<int, string, bool(*)(int, int)> mmid;
mmid pairs(ptr);
string s1;
ifstream inFile("dict.txt", ios::in | ios::binary);
while (cin>>s1) {
int f =translate(s1);
pairs.insert(mmid::value_type(f, s1));
}
int m;
cin >> m;
multimap<int, string>::iterator it;
while (true) {
it = pairs.find(m);
if (it != pairs.end()) {
cout << it->second << endl;
pairs.erase(it);
}
else {
break;
}
}
return 0;
}
当我尝试输入时"ANDY HARRY",当它处理到第二个输入时,
pairs.insert(mmid::value_type(f, s1));
在这一行,它给了我代码 0xC0000005:访问冲突执行位置 0x00000000。我的代码有什么问题?为什么它不是在第一个条目上而是在第二个条目上给我错误消息?谢谢
我认为问题出在这段代码中:
bool(*ptr)(int, int);
mmid pairs(ptr);
在第一行中,您定义了一个名为 ptr
的函数指针,但未对其进行初始化。这意味着当您使用 ptr
初始化 pairs
时,您正在使用垃圾比较函数指针对其进行初始化。
要解决此问题,请传入您要使用的实际比较函数。例如:
mmid pairs(mycompare);
此外,这段代码非常可疑:
multimap<int, string>::iterator it;
it = pairs.find(m);
请注意 multimap
的类型与 pairs
的类型不匹配,因此没有先验理由怀疑此迭代器会正常工作。考虑一下:
mmid::iterator it;
我正在为 USACO 培训页面中的一个问题编写代码,当我尝试调试我的代码时:
#include <iostream>
#include<fstream>
#include<string>
#include<map>
#include<math.h>
using namespace std;
int translate(string &s) {
int n = s.length();
int result = 0;
for (int i = 0; i < s.length(); i++) {
int n1 = 0;
if (s.at(i) == 'Q' || s.at(i) == 'Z') {
return -1;
}
switch (s.at(i)) {
case 'A':
case 'B':
case'C':
result = result + 2 * pow(10, n-1);
n--;
break;
case 'D':
case 'E':
case'F':
result = result + 3 * pow(10, n - 1);
n--;
break;
case 'G':
case 'H':
case 'I':
result = result + 4 * pow(10, n - 1);
n--;
break;
case 'J':
case 'K':
case 'L':
result = result + 5 * pow(10, n - 1);
n--;
break;
case 'M':
case 'N':
case 'O':
result = result + 6 * pow(10, n - 1);
n--;
break;
case 'P':
case 'R':
case 'S':
result = result + 7 * pow(10, n - 1);
n--;
break;
case 'T':
case 'U':
case 'V':
result = result + 8 * pow(10, n - 1);
n--;
break;
case 'W':
case 'X':
case 'Y':
result = result + 9 * pow(10, n - 1);
n--;
break;
}
}
return result;
}
bool mycompare(int n, int m) {
string a, b;
a = to_string(n);
b = to_string(m);
if (a < b) {
return true;
}
else return false;
}
int main() {
bool(*ptr)(int, int);
typedef multimap<int, string, bool(*)(int, int)> mmid;
mmid pairs(ptr);
string s1;
ifstream inFile("dict.txt", ios::in | ios::binary);
while (cin>>s1) {
int f =translate(s1);
pairs.insert(mmid::value_type(f, s1));
}
int m;
cin >> m;
multimap<int, string>::iterator it;
while (true) {
it = pairs.find(m);
if (it != pairs.end()) {
cout << it->second << endl;
pairs.erase(it);
}
else {
break;
}
}
return 0;
}
当我尝试输入时"ANDY HARRY",当它处理到第二个输入时,
pairs.insert(mmid::value_type(f, s1));
在这一行,它给了我代码 0xC0000005:访问冲突执行位置 0x00000000。我的代码有什么问题?为什么它不是在第一个条目上而是在第二个条目上给我错误消息?谢谢
我认为问题出在这段代码中:
bool(*ptr)(int, int);
mmid pairs(ptr);
在第一行中,您定义了一个名为 ptr
的函数指针,但未对其进行初始化。这意味着当您使用 ptr
初始化 pairs
时,您正在使用垃圾比较函数指针对其进行初始化。
要解决此问题,请传入您要使用的实际比较函数。例如:
mmid pairs(mycompare);
此外,这段代码非常可疑:
multimap<int, string>::iterator it;
it = pairs.find(m);
请注意 multimap
的类型与 pairs
的类型不匹配,因此没有先验理由怀疑此迭代器会正常工作。考虑一下:
mmid::iterator it;