C++超出范围错误
C++ Out of Range Error
好的,所以当我尝试运行代码并且无法弄清楚如何解决它时,我不断收到此错误。(在 hw6.exe 中0x75195608处未处理的异常:内存位置 0x0101F850 处Microsoft C++异常:std::out_of_range。\我在下面包含了我的源代码。此外,我正在读取的文件根本不长,所以我认为这不是问题所在。
int main() {
//Initializes all of the variables, strings,boolean, and vectors
ifstream inFS;
int count = 1;
int i = 0;
int j = 0;
int location = 0;
char ch;
bool marker = 0;
string filename = "hw6-Fall2017.txt";
string list = "ABCDEFGHIJKEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
vector<int> locations;
vector<int> find_Locations;
vector<char> notFound;
vector<char> Found;
//Iterates through the file searching for each of the characters
while (count <= 62) {
inFS.open(filename);
if (!inFS.is_open()) {
cout << "Could not open the file: " << filename << endl;
return 1;
}
while (inFS.get(ch) && marker == 0) {
location++;
if (ch == list[i]) {
marker = 1;
}
}
inFS.close();
//Sets characters not found to have a location of 0
if (marker == 0) {
location = 0;
}
locations.push_back(location);
marker = 0;
location = 0;
i++;
count++;
}
//Creates a table printing out the characters and their susequent locations
for (i = 0;i < list.size();i++) {
if (locations.at(i) == 0) {
cout << list[i] << " " << setw(6) << "NotFnd"<< " ";
notFound.push_back(list[i]);
}
else {
cout << list[i] << " " << setw(6) << locations.at(i) << " ";
find_Locations.push_back(locations.at(i));
}
j++;
if (j == 5) {
cout << endl;
j = 0;
}
}
cout << endl << endl << endl;
//Sorts the characters in the order that they were found
sort(find_Locations.begin(), find_Locations.end());
for (i = 0;i < find_Locations.size();i++) {
for (j = 0;j < locations.size();j++) {
if (find_Locations.at(i) == locations.at(j) && marker == 0) {
Found.push_back(list[j]);
j = locations.size();
}
}
}
count = 0;
j = 0;
//Creates a table printing out the characters in the oreder they were found
//in the text file along with their locations. Characters not found are
//displayed first with a location of "NotFnd".
for (i = 0;i < (Found.size() + notFound.size());i++) {
if (i < Found.size()) {
cout << Found.at(i) << " " << setw(6) << find_Locations.at(i)<< " ";
}
else {
cout << notFound.at(j) << " " << setw(6) << "NotFnd" << " ";
j++;
}
count++;
if (count == 5) {
cout << endl;
count = 0;
}
}
system("pause");
return 0;
}
通过代码审查不容易找到答案。
但这行本身看起来不错
string list = "ABCDEFGHIJKEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
但与此一起
while (count <= 62) {
看起来很可疑,我认为应该是
while (count < list.size()) { // 2*26+10==62
"关闭一个错误",可能会导致此处出现问题
for (i = 0;i < find_Locations.size();i++) {
for (j = 0;j < locations.size();j++) {
if (find_Locations.at(i) == locations.at(j) && marker == 0) {
Found.push_back(list[j]); // <--- if J>=list.size()
j = locations.size();
}
}
}
并在标记线上发生潜在的崩溃。
但真正的错误就在这里
Found.push_back(list[j]); // j should have been i
这应该会导致崩溃
cout << Found.at(i) << " " << setw(6) << find_Locations.at(i)<< " ";
相关文章:
- 错误:未在此范围内声明'reverse'
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- 错误:"imread"未在此范围内声明
- 我在范围内未声明的错误类有问题
- 未在此范围内声明错误 'xy'
- 在C++中使用变量而不是"#define"来指定数组大小是不是一种糟糕的做法?(C错误:在文件范围内
- 运行时错误:矢量下标超出范围:正在检查空集
- 错误:'[' 之前预期的非限定 id 和错误:'users'未在此范围内声明
- 在此范围内未声明错误"Clreol"
- "myClock"未在此范围错误中声明
- 包含来自另一个文件的函数会导致范围错误(openFoam)
- 构造中错误:未在此范围中声明"data"
- 使用基于范围的指针循环和向量时出现C++错误
- DEV C++ 第 69 行 [错误] ISO "for" 范围更改了"i"的名称查找 [-允许]
- C++ 中的类之间的数据重新循环 - 错误:'<class name>'未在此范围内声明
- 使用to_string、reverse、stoi组合的C++反转编号给出运行时错误实例超出范围
- 如何避免指针超出范围(多态性)的C++分段错误
- 代码作为 C 文件工作,但不作为C++文件,错误:'__builtin_types_compatible_p'未在此范围内声明
- 如何修复范围内的字符串声明错误
- 如何修复错误:"vreinterpretq_u32_f64"未在此范围内声明 - 在Android上使用Eigen构建