当我尝试运行此程序时,我收到一个错误,该程序暂停程序并说“向量下标超出范围”
知道我做错了什么吗?
- #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中插入任何东西,所以行字[0];是非法的,因为它访问它的第一个元素,它不存在.