我在迭代文件名向量时遇到了这个段错误.std :: vector由另一个在相当混乱的代码中读取csv的函数填充.所以我把它缩小到下面的代码导致问题.
向量segfaults的迭代器在产生4个项目的向量的第一个(有时是更晚的)项目之后.推送第5项修复了问题.奇怪?矢量的迭代器工作正常.
#include <iostream>
#include <vector>
using namespace std;
std::vector<int> popbar() {
// populate vector of integers
//
std::vector<int> bar;
for(int i = 1; i < 6; i++)
bar.push_back(i);
return bar;
}
std::vector<std::string> popxar() {
// populate vector of strings
//
std::vector<std::string> xar;
xar.push_back("one");
xar.push_back("two");
xar.push_back("three");
xar.push_back("four");
// this line fixes segfault
//xar.push_back("five");
return xar;
}
void foo () {
// yield next vector item
//
//auto bar = popbar();
auto bar = popxar();
//static auto itx = …Run Code Online (Sandbox Code Playgroud)