获取“以std :: length_error:vector类型的未捕获异常终止”错误C ++

时间:2015-08-25 16:57:14

标签: c++ c++11 vector

我正在编写一个程序来帮助解决填字游戏。所以我从英语中所有单词的文本列表中得到一个单词,使每个单词成为字符的向量,并将该向量与我所拥有的任何起始字母的向量进行比较。它运行良好,给我很好的输出,但每次我收到错误“libc ++ abi.dylib:终止与std :: length_error:vector类型的未捕获异常。”

这是我的代码:

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
#include <iterator>

using namespace std;

string getLetters() {
    string word; // Get user letter, put in variable word
    cout << "Enter a set of letters" << endl;
    cin >> word;
    return word;
}

int getLengthOfWord() {
    int length; // Get length of word
    cout << "Enter the number of letters in the word" << endl;
    cin >> length;
    return length;
}

// Change strings to vectors of chars
vector<char> stringToVector(string word) {
    std::vector<char> v(word.begin(), word.end());
    return v;
}

bool compareVectors(vector<char> userWord, vector<char> listWord, int length) {

    if (listWord.size() != length) // Make sure the word from the list is the right length
    {
        return false;
    }

    int counter = 0; // Counter

    for (int i = 0; i < userWord.size(); i++) { // Iterating through the two words
        for (int j = 0; j < listWord.size(); j++) {
            if (listWord[j] == userWord[i]) { // If the letters match
                listWord.erase(listWord.begin() - 1 + j); // Erase the letter from the word
                counter++; // Increase counter
                break; // Break out of for loop
            }
        }
    }

    if (counter == userWord.size()) { // If there were as many matches as letters in user set
        return true;
    }
    else {
        return false;
    }

}

int main() {

    string example; // variable to put words
    ifstream wordList; // New ifstream object
    wordList.open("/Users/alexray/Dropbox/C++ Practice/WordJumbleSolver/wordsEn.txt"); //open word list

    int length = getLengthOfWord(); // Get user input
    string word = getLetters();

    vector<char> vector1(stringToVector(word));

    while (wordList.is_open()) {
        getline(wordList, example); // Get word, put it in example variable

        vector<char> vector2(stringToVector(example)); // Make word from list a vector
        vector2.erase(vector2.end() - 1); // Erase escape character from end of word
        if(compareVectors(vector1, vector2, length)) { // compare the vectors
            cout << example << endl;
        }
    }
    wordList.close(); // Close stream

    return 0;

}

从谷歌搜索,我认为这是我的矢量最初不足以处理一些单词的问题,但在向矢量赋值之前做vector.reserve(some_number)没有任何帮助。另外,我无法想象一个向量与&lt; 20个元素有任何问题。

感谢您的帮助! (我是C ++的新手,所以如果有什么我显然应该采取不同的做法,请告诉我。)

编辑:我正在使用的文件是此网站上的wordsEn.txt文件:http://www-01.sil.org/linguistics/wordlists/english/

2 个答案:

答案 0 :(得分:1)

就我而言,这是两个 vcxproj 项目中 C++ standard 之间的不匹配。 我只是简单地将两个项目与相同的 C++ 标准 (17) 对齐,并且成功了。

项目 ➤ PropertiesC/C++LanguageC++ Language Standard

答案 1 :(得分:0)

我一直在寻找错误的地方。我查看了文件中的单词/行数(109582)并更改了

while (wordList.is_open()) {
        getline(wordList, example); // Get word, put it in example variable

        vector<char> vector2(stringToVector(example)); // Make word from list a vector
        vector2.erase(vector2.end() - 1); // Erase escape character from end of word
        if(compareVectors(vector1, vector2, length)) { // compare the vectors
            cout << example << endl;
        }
        counter++;
    }

while (counter < 109582) {
        getline(wordList, example); // Get word, put it in example variable

        vector<char> vector2(stringToVector(example)); // Make word from list a vector
        vector2.erase(vector2.end() - 1); // Erase escape character from end of word
        if(compareVectors(vector1, vector2, length)) { // compare the vectors
            cout << example << endl;
        }
        counter++;
    }

似乎我通过尝试读取比文件中可用的行更多的行来获得某种溢出错误。