C++超出范围错误

C++ Out of Range Error

本文关键字:错误 范围 C++      更新时间:2023-10-16

好的,所以当我尝试运行代码并且无法弄清楚如何解决它时,我不断收到此错误。(在 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)<< " ";