我正在尝试将智能指针的向量排序到类.我使用结构作为第三个参数来std::sort用operator():
struct PhraseSmartPtrParseCreationComparer
{
bool operator()(const std::shared_ptr<Phrase>& a, const std::shared_ptr<Phrase>& b)
{
if (a.get() == nullptr)
return b.get() != nullptr;
if (b.get() == nullptr)
return a.get() != nullptr;
return *a < *b;
}
};
Run Code Online (Sandbox Code Playgroud)
偶尔,我得到一个分段错误,其中比较方法中的一个指针指向无效的结构; 总是一个.有趣的是,在排序之前,所有对象都是完整的; 我也尝试修改函数来删除引用位:const std::shared_ptr<Phrase> a,并在其他地方崩溃.
电话没什么特别的:
std::sort(_detectedPhrases.begin(), _detectedPhrases.end(), PhraseSmartPtrParseCreationComparer());
Run Code Online (Sandbox Code Playgroud)
有什么我想念的东西或者我应该在其他地方寻找吗?