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

时间:2019-04-26 05:04:04

标签: c++ visual-studio vector

我遇到了向量下标超出范围的错误,但是我无法弄清楚错误在哪里,有6个函数加上main函数。一些代码被省略。编写代码时有什么需要注意的地方,可以避免此错误?

功能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;
}

功能2

vector<string> grabReplies(vector<string> fileRead, int index) {

    vector<string> replies;

    for (size_t i = index + 1; i < fileRead.size(); i++) {
        while (fileRead[i].find(out) != string::npos) {
            replies.push_back(fileRead[i]);
        }
    }
    return replies;
}

功能3

string pickReply(vector<string> replies) {
    string reply;

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

    return replies[item];
}

功能4

vector<string> fileRead(string fileDir)
{
    vector<string> fileRead;            
    string line;                        

    ifstream rawFile(fileDir);          

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

    return fileRead;                    
}

功能5

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;               
}

功能6

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;                                           
}

0 个答案:

没有答案