terminate called after throwing an instance of 'std::length_error'

匿名 (未验证) 提交于 2019-12-03 00:50:01

问题:

this is my first post here. As i am newbie, the problem might be stupid. I was writing a piece of code while the following error message shown,

terminate called after throwing an instance of 'std::length_error'

what(): basic_string::_S_create

/home/gcj/finals /home/gcj/quals where Aborted

the following is the offending code especially Line 39 to Line 52. It is weired for me as this block of code is almost same as the Line64 to Line79.

   int main(){   std::vector<std::string> dirs, need; std::string tmp_str; std::ifstream fp_in("small.in"); std::ofstream fp_out("output"); std::string::iterator iter_substr_begin, iter_substr_end; std::string slash("/");  int T, N, M;  fp_in>>T; for (int t = 0; t < T; t++){   std::cout<<" time "<< t << std::endl;    fp_in >> N >> M;   for (int n =0; n<N; n++){     fp_in>>tmp_str;     dirs.push_back(tmp_str);     tmp_str.clear();   }   for (int m=0; m<M; m++){     fp_in>>tmp_str;     need.push_back(tmp_str);     tmp_str.clear();   }     for (std::vector<std::string>::iterator iter = dirs.begin(); iter!=dirs.end(); iter++){     for (std::string::iterator iter_str = (*iter).begin()+1; iter_str<(*iter).end(); ++iter_str){   if ((*iter_str)=='/') {     std::string tmp_str2((*iter).begin(), iter_str);     if (find(dirs.begin(), dirs.end(), tmp_str2)==dirs.end()) {       dirs.push_back(tmp_str2);     }    }      }    }    for (std::vector<std::string>::iterator iter_tmp = dirs.begin(); iter_tmp!= dirs.end(); ++iter_tmp)     std::cout<<*iter_tmp<<" ";     dirs.clear();   std::cout<<std::endl;   std::cout<<" need "<<std::endl;    //processing the next     for (std::vector<std::string>::iterator iter_tmp = need.begin(); iter_tmp!=need.end(); ++iter_tmp)     std::cout<<*iter_tmp<<" ";    std::cout<<"  where ";    for (std::vector<std::string>::iterator iter = need.begin(); iter!=need.end(); iter++){     for (std::string::iterator iter_str = (*iter).begin()+1; iter_str<(*iter).end(); ++iter_str){   if ((*iter_str)=='/') {     std::string tmp_str2((*iter).begin(), iter_str);     if (find(need.begin(), need.end(), tmp_str2)==need.end()) {       need.push_back(tmp_str2);     }     }      }    }    for (std::vector<std::string>::iterator iter_tmp = need.begin(); iter_tmp!= need.end(); ++iter_tmp)     std::cout<<*iter_tmp<<" ";     need.clear();   std::cout<<std::endl;     //finish processing the next   }   for (std::vector<std::string>::iterator iter= dirs.begin(); iter!=dirs.end(); iter++)   std::cout<<*iter<<" "; std::cout<<std::endl;    for (std::vector<std::string>::iterator iter= need.begin(); iter!=need.end(); iter++)   std::cout<<*iter<<" "; std::cout<<std::endl;   fp_out.close();   } 

best regards, Mark

回答1:

You are adding items to the dirs and need vectors while iterating over them. This is not allowed: if adding an item requires a reallocation, it will invalidate all existing iterators, and can cause various errors when you next access them.



回答2:

I don't know what is proper way to do it, but since it was hard to find this answer, I'll put it here. "terminate called after throwing 'foo'" just means that the exception was not catched by any try ... catch clause.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!