向量下标超出范围错误,C ++ [英] Vector subscript out of range error, C++

查看:89
本文介绍了向量下标超出范围错误,C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试运行该程序时,出现错误,导致程序停止运行并显示向量下标超出范围"

When I try to run this program I get an error that halts the program and says, "Vector subscript out of range"

知道我在做什么错吗?

#include <vector>
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;

//(int argc, char* argv[]
int main()
{
    fstream bookread("test.txt");
    vector<string> words;

    bookread.open("test.txt");
    if(bookread.is_open()){
        cout << "opening textfile";
        while(bookread.good()){
            string input;
            //getline(bookread, input);
            bookread>>input;
            //string cleanedWord=preprocess(input);         
            //char first=cleanedWord[0];
            //if(first<=*/
            //cout << "getting words";
            //getWords(words, input);
        }
    }
    cout << "all done";

    words[0];

getchar();
}

推荐答案

您永远不要在单词vector中插入任何内容,因此行words[0];是非法的,因为它访问了它的第一个元素,该元素不存在

You never insert anything into the words vector, so the line words[0]; is illegal, because it accesses the first element of it, which does not exist.

这篇关于向量下标超出范围错误,C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆