C++ 表达式:Visual Studio中的向量下标超出范围错误

C++ 表达式:Visual Studio中的向量下标超出范围错误,c++,visual-studio,vector,C++,Visual Studio,Vector,我遇到了向量下标超出范围的错误,但我无法找出错误在哪里,有6个函数加上主函数。一些代码被省略了。在编写代码时,我是否可以注意避免此错误 职能1 int matchInput(vector<string> fileRead, int index) { for (size_t i = index; i < fileRead.size(); i++) { if (fileRead[i].find(in) != string::npos) {

我遇到了向量下标超出范围的错误,但我无法找出错误在哪里,有6个函数加上主函数。一些代码被省略了。在编写代码时,我是否可以注意避免此错误

职能1

int matchInput(vector<string> fileRead, int index) {
    for (size_t i = index; i < fileRead.size(); i++) {
        if (fileRead[i].find(in) != string::npos) {
            return i;
        }
    }
    return 0;
}
主要功能

int main() {

fileDir = "../src/reply.txt";                       
fileVector = fileRead(fileDir);

cout << "Welcome to the ChatBot" << endl            
     << "a program by 27023119"  << endl << endl
     << "Please ask me anything" << endl
     << "to end chat just enter quit"    << endl;

while(1)                                            
{
    index = 0;
    cout << ">";                                
    getline(cin, userInput);                    

    if (userInput == "quit"){                   
        cout << "Thanks for using Chat Bot!" << endl;   
        break;                                          
    }

    index = matchInput(fileVector, index);

    if (wordMatch(userInput, fileVector, index) == true) {
        replies = grabReplies(fileVector, index);
        reply = pickReply(replies);
        cout << sanitising(reply) << endl;
    }
    else {
        cout << "Sorry I don't understand." << endl;
    }
}

return 0;                                           
}
intmain(){
fileDir=“../src/reply.txt”;
fileVector=fileRead(fileDir);

你能给出回溯吗?你试过使用调试器吗?它是在visual studio中完成的,当遇到这个错误时,它根本不允许跟踪,我应该试着用g++调试它吗?但是,g++通常不会给出关于向量子脚本的错误,除非你找到属于你的代码。@LeoWang,但我不能找出错误所在--Visual Studio调试器会准确地告诉您问题所在。查看调用堆栈,转到您编写的函数,然后查看
索引
。向量大小超出了条目数的界限。为什么会这样?您的工作是查看代码并找出向量的原因正在使用一个超出范围的索引,即调试代码。您能给出回溯吗?您尝试过使用调试器吗?这是在visual studio中完成的,当遇到此错误时,根本不允许跟踪,我是否应该尝试使用g++调试它?但是,g++通常不会给出有关向量子脚本的错误向下滚动调用堆栈窗口直到您找到属于您的代码。@LeoWang但我无法找出错误所在--Visual Studio调试器会准确地告诉您问题所在。请查看调用堆栈,转到您编写的函数,然后查看
index
。向量大小超出了条目数的范围。为什么?这是您的问题所在查看代码并找出向量使用超出界限的索引的原因,即调试代码。
string pickReply(vector<string> replies) {
    string reply;

    int item = rand() % replies.size();

    return replies[item];
}
vector<string> fileRead(string fileDir)
{
    vector<string> fileRead;            
    string line;                        

    ifstream rawFile(fileDir);          

    while (getline(rawFile, line)) {    
        fileRead.push_back(line);       
    }

    return fileRead;                    
}
bool wordMatch(string userInput, vector<string> fileRead, int index)    
{
    string delimiter = " ";             
    vector<string> wordsUserInput;      
    vector<string> wordsOnFile;         
    string s;                           
    string token;                       

    s = userInput;                      

    size_t pos = 0;                     
    token = "";                         
    while ((pos = s.find(delimiter)) != string::npos) {     
        token = s.substr(0, pos);                           
        wordsUserInput.push_back(token);                    
        s.erase(0, pos + delimiter.length());               
    }

    s = fileRead[index];            //s set to on file record

    pos = 0;                        //reset position counter
    token = "";                         //reset token
    while ((pos = s.find(delimiter)) != string::npos) {     
        token = s.substr(0, pos);                           
        wordsOnFile.push_back(token);                       
        s.erase(0, pos + delimiter.length());               
    }

    for (size_t i = 0; i < wordsUserInput.size(); i++) {    
        for (size_t j = 0; j < wordsOnFile.size(); j++) {
            if (wordsUserInput[i] == wordsOnFile[j]) {      
                return true;    
            }
        }
    }
    return false;               
}
string sanitising(string dirty) {
    string clean;           
    clean = dirty.erase(0, 8);      
    return clean;           
}
int main() {

fileDir = "../src/reply.txt";                       
fileVector = fileRead(fileDir);

cout << "Welcome to the ChatBot" << endl            
     << "a program by 27023119"  << endl << endl
     << "Please ask me anything" << endl
     << "to end chat just enter quit"    << endl;

while(1)                                            
{
    index = 0;
    cout << ">";                                
    getline(cin, userInput);                    

    if (userInput == "quit"){                   
        cout << "Thanks for using Chat Bot!" << endl;   
        break;                                          
    }

    index = matchInput(fileVector, index);

    if (wordMatch(userInput, fileVector, index) == true) {
        replies = grabReplies(fileVector, index);
        reply = pickReply(replies);
        cout << sanitising(reply) << endl;
    }
    else {
        cout << "Sorry I don't understand." << endl;
    }
}

return 0;                                           
}