Ofstream trouble,我该如何解决这个问题?
ofstream trouble, how do i resolve this?
我在一个包含单词作为键的映射上迭代,每个单词都有一堆分配的数字存储在vector<int>
中,指示在哪一行找到它们。我在。txt文件中输出该信息时遇到了麻烦。
在文件中出现足够多的单词垃圾后,程序崩溃。
经过一些简短的测试,我发现错误发生在明显的地方,即输出最终信息到。txt文件时。
在谷歌了一下之后,唯一相关的信息片段是有人简短地说不应该在循环中使用ofstream。我猜这可能会导致溢出?也许是什么东西没被冲走?
那么哪里出了问题,为什么?我该如何解决这个问题?
//FINAL OUTPUT TO A FILE, CREATING A NEW .txt FILE
ofstream filehandle;
filehandle.open("result.txt",ios::out);
if( filehandle.is_open() )
{
for(map<string, vector<int> >::const_iterator a = line_num_by_word.begin();a!=line_num_by_word.end();++a)
{
filehandle << "The word " << """ << a->first << """ << " is present on: n";
vector<int>::const_iterator int_vec_c_it = a->second.begin();
while( int_vec_c_it != a->second.end() )
{
vector<int>::const_iterator in_front_of_it = int_vec_c_it+1;
//count the number of times
int counter = 1;
while( *(int_vec_c_it)==*(in_front_of_it) )
{
++counter;
++in_front_of_it;
}
filehandle << " line " << *(int_vec_c_it) << "-" << counter << " times ";
int_vec_c_it = in_front_of_it;
}
filehandle << "n";
}
}else cout << "no output" <<endl;
当a->second()在end()之前是1时,it_front_of_it是end()。然后在while语句中解引用它。
while( *(int_vec_c_it)==*(in_front_of_it) )
相关文章:
- Project Euler问题4的错误解决方案
- 在java中解决这段代码时面临循环中的问题
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 难以理解某些人解决IOI问题的源代码
- 节俭并发:未解决的外部问题
- IpOpt拒绝解决不受约束的问题
- 错误LNK2005无法解决问题的解决方案
- 如何解决g++问题"internal compiler error: Illegal instruction min() _GLIBCXX_USE_NOEXCEPT { return __FLT_MI
- 必须使用尾随返回类型的示例,因为无法用旧方法解决问题
- 如何解决问题"unresolved extenal symbol _DllMainCRTStartup@12"?
- 如何使用OlesetClipboard将我的COM对象放置在剪贴板上时解决问题
- 与C++析构函数相比,C#析构函数和GC并没有真正解决问题
- 没有调用函数,而是使用垃圾值.如何调用函数并解决问题
- 所有的组合算法和解决C++问题的一般方法
- 模板过载解决问题
- 对"pthread_key_create"的未定义引用;参数顺序不能解决问题
- 如何在使用cplex解决问题时设置间隙
- 选择一门语言学习真的很重要吗?他们都使用相似的技术来解决问题
- Iostream解决c++问题
- 递归是一件坏事吗?在递归似乎最有用的地方,是否有更清晰或更有效的方法来解决问题?