向量下标超出范围错误,C

前端之家收集整理的这篇文章主要介绍了向量下标超出范围错误,C前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我尝试运行此程序时,我收到一个错误,该程序暂停程序并说“向量下标超出范围”

知道我做错了什么吗?

  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <fstream>
  6. #include <sstream>
  7. using namespace std;
  8.  
  9. //(int argc,char* argv[]
  10. int main()
  11. {
  12. fstream bookread("test.txt");
  13. vector<string> words;
  14.  
  15. bookread.open("test.txt");
  16. if(bookread.is_open()){
  17. cout << "opening textfile";
  18. while(bookread.good()){
  19. string input;
  20. //getline(bookread,input);
  21. bookread>>input;
  22. //string cleanedWord=preprocess(input);
  23. //char first=cleanedWord[0];
  24. //if(first<=*/
  25. //cout << "getting words";
  26. //getWords(words,input);
  27. }
  28. }
  29. cout << "all done";
  30.  
  31. words[0];
  32.  
  33. getchar();
  34. }

解决方法

你永远不会在单词vector中插入任何东西,所以行字[0];是非法的,因为它访问它的第一个元素,它不存在.

猜你在找的C&C++相关文章