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

时间:2019-07-13 01:38:46

标签: c++ vector

调试断言失败的向量下标超出范围,我不确定此错误的确切来源,我将其范围缩小到此循环。

我已经仔细检查了循环条件,它们看起来还不错 我还放入了一些测试代码,此循环之前的所有内容都运行良好。

我解决了这个问题! 谢谢大家的帮助!

编辑:工作代码

    #include "stdafx.h"
    #include <string>
    #include <vector>
    #include <iostream>

    using namespace std;

    vector<string> sVec, sVecTemp;
    string s, str, strTemp1, strTemp2, strTemp3;
    string::iterator it;
    string::reverse_iterator rit;
    bool rev = 0, upCase = 0, noVowel = 0;

    int main(){
        cout << "Enter a Sentence, using Enter as a Space, Ending with a Period" << endl;

        while (s != "."){
            cin >> s;
            sVec.push_back(s);
        }

        cout << endl << "Your Sentence: ";

        for (int i = 0; i != (sVec.size() - 1); ++i){
            cout << sVec[i] << " ";
        }

        cout << endl << "Updated Sentance: ";

        for (int i = 0; i != (sVec.size() - 1); ++i){

            str = sVec[i];

            if (sVec[i].size() % 2 == 0){
                rev = !rev;
            }
            if (sVec[i].size() % 3 == 0){
                upCase = !upCase;
            }
            if (sVec[i].size() % 5 == 0){
                noVowel = !noVowel;
            }
            if (noVowel == 1){
                for (it = str.begin(); it != str.end(); ++it){
                    if (*it != 'a' && *it != 'A' && *it != 'e' &&
                        *it != 'E' && *it != 'i' && *it != 'I' &&
                        *it != 'o' && *it != 'O' && *it != 'u' &&
                        *it != 'U'){
                        strTemp1 += *it;
                    }
                }
            }
            else{
                for (it = str.begin(); it != str.end(); ++it){
                    strTemp1 += *it;
                }
            }
            if (upCase == 1){
                for (it = strTemp1.begin(); it != strTemp1.end(); ++it){
                    strTemp2 += (toupper(*it));
                }
            }
            else{
                for (it = strTemp1.begin(); it != strTemp1.end(); ++it){
                    strTemp2 += *it;
                }
            }
            if (rev == 1){
                for (rit = strTemp2.rbegin(); rit != strTemp2.rend(); ++rit){
                    strTemp3 += *rit;
                }
            }
            else{
                for (it = strTemp2.begin(); it != strTemp2.end(); ++it){
                    strTemp3 += *it;
                }
            }
            cout << strTemp3 << " ";

            strTemp1.clear();
            strTemp2.clear();
            strTemp3.clear();
        }
        return 0;
    }

0 个答案:

没有答案