Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;向后推,向后弹出。获取错误:向量下标超出范围_C++_Loops_Vector - Fatal编程技术网

C++ C++;向后推,向后弹出。获取错误:向量下标超出范围

C++ C++;向后推,向后弹出。获取错误:向量下标超出范围,c++,loops,vector,C++,Loops,Vector,如果有人可以查看我的代码,我似乎找不到它的问题,但我确信我对push_-back/pop_-back函数不太了解 该程序旨在创建一个向量,该向量接受用户输入,并在用户键入“停止”时停止 这肯定是pop_back给了我这个错误,但我不确定我做错了什么或如何改变它,任何帮助都是感激的 代码: #include <vector> #include <string> using namespace std; int main(){ vector<string&

如果有人可以查看我的代码,我似乎找不到它的问题,但我确信我对push_-back/pop_-back函数不太了解

该程序旨在创建一个向量,该向量接受用户输入,并在用户键入“停止”时停止

这肯定是pop_back给了我这个错误,但我不确定我做错了什么或如何改变它,任何帮助都是感激的

代码:

#include <vector>
#include <string>
using namespace std;


int main(){

    vector<string> animals;

    vector<string> ages;
    int const SIZE = 5;
    string stopCheck = "stop";
    string tempUserInput;
    int i = 0;

    //Loop used to gather user input and store it into array, animals and array, ages
    cout << "Please enter up to 5 animal names and ages, or type 'stop' to end" << endl;

    while( i<5 ) {
        cout << i << endl;
        // Taking user input for NAMES

        cout << "Enter the name of an animal: ";
        cin >> tempUserInput;
        animals.push_back(tempUserInput);
        cout << endl;

        cout << animals.size() << endl;

        //Function that automates the tolower function by applying it to each    element automatically
        //transform(animals[i].begin(), animals[i].end(), animals[i].begin(), tolower);

        cout << animals.size() << endl;

        //Checking for a stop input
        if (!animals[i].compare(stopCheck)){
            animals.pop_back();
            i = 5;
        }
        i++;

        return 0;
    }
#包括
#包括
使用名称空间std;
int main(){
媒介动物;
媒介年龄;
int const SIZE=5;
字符串stopCheck=“stop”;
字符串tempUserInput;
int i=0;
//用于收集用户输入并将其存储到数组、动物和数组、年龄中的循环

如果(tempUserInput==stopCheck){break;}
调试器应该告诉您错误发生的行,“向量下标超出范围”意味着您做了类似于
某个向量[索引]
的事情,其中
索引
某个向量大小()
,意思是你访问了一个不存在的元素。你到底有什么问题?在
推回
之前,如果(tempUserInput==stopCheck){break;}
你的调试器应该告诉你错误发生的行。“向量下标超出范围”意味着你做了类似
的事情[index]
其中
index
是>=
一些向量.size()
,这意味着您访问了一个不存在的元素。您到底有什么问题?