bai*_*ibo 0 c++ sorting stl segmentation-fault
这是我的代码
bool cmp (const char &a, const char &b)
{
if ((int) a == (int) b)
{
return false;
}
if ((int) a > (int) b)
{
return false;
}
return true;
}
std::sort(
dfaVector.at(0).getSigma().begin(),
dfaVector.at(0).getSigma().end(),
cmp);
Run Code Online (Sandbox Code Playgroud)
getSigma()返回std::vector<char>,它们不是空的 - 我检查了一下.如果你愿意,我可以从gdb发布堆栈跟踪.我正在使用g ++ 4.8,OS Mint 14
回答
正如@livingissuicide所建议的那样,问题是getSigma()需要返回一个引用(即......常量,@ PhoenixX_2).解释为什么 它需要返回一个引用(以及为什么只是一个简单的副本是不够的)是因为
问题是有两个getSigma调用,产生两个不同的向量.传递给sort的一对迭代器不是有效范围 - 两个迭代器指向不同的容器.
解释由@IgorTandetnik提供.
getSigma()返回std :: vector <char>
确保你的getSigma()函数返回一个引用(std :: vector <char>&)而不是副本!