显示expression :vector subscript out of range错误 求 大神知道

xlxu_12 2016-04-08 11:56:53
#include "StdAfx.h"
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include<vector>
#include <cstdlib>
#include <sstream>
using namespace std;
void splitdou(string s,vector<string>&ret)
{

size_t last= 0;
size_t index=s.find_first_of(',',last);
while (index!=std::string::npos)
{
ret.push_back(s.substr(last,index-last));
last=index+1;
index=s.find_first_of(',',last);
}
if (index-last>0)
{
ret.push_back(s.substr(last,index-last));
}

}

int main(){
ifstream in;
string line;
in.open("D:\\text\\traver.txt");

while(in>>line)
{
vector<string> str;
str=vector<string>();
splitdou(line,str);//用逗号隔开分离;

for(int j1=0;j1<str.size();j1++)//测试后期去掉;
{
cout<<str[j1]<<endl;
}
int traveller;
int flightnumber;
char departure[20];
char destination[20];

traveller=atoi(str[0].c_str());//注意字符转换;

flightnumber=atoi(str[5].c_str());//chu


strcpy(departure,str[6].c_str());

strcpy(destination,str[7].c_str());
//对str[i]计算结果;输出结果
//i++;
cout<<traveller<<endl;
cout<<flightnumber<<endl;
cout<<departure<<endl;
cout<<destination<<endl;

}
}
...全文
885 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xlxu_12 2016-04-09
  • 打赏
  • 举报
回复
已经解决了 是越界 加入 str.resize( 200); 解决;这个错误我就找了两天 气死
小灸舞 2016-04-09
  • 打赏
  • 举报
回复
是读第二行数据的时候崩的? 楼主要不贴几行文件里的内容
xlxu_12 2016-04-09
  • 打赏
  • 举报
回复
while 循环了一边是可以执行的 但是第二遍的时候就出现错误。
xlxu_12 2016-04-08
  • 打赏
  • 举报
回复
菜鸟新人 第一次发帖 多多关照。
小灸舞 2016-04-08
  • 打赏
  • 举报
回复 1
这个错误一般都是越界造成的,比如你的vector只有5个元素,你却用下标访问第6个或者-1个都会造成这个错误 崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。
赵4老师 2016-04-08
  • 打赏
  • 举报
回复
“多一少一”问题占程序员常犯错误的10%以上! 避免“多一少一”问题的方法之一是将比如<10甚至<5的数代入程序片断,搬手指头心算验证一下程序到底应该写为 x、x-1、x+1中的哪个? <、<=、==、>、>=中的哪个?

64,690

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧