C++ 基于多个条件对结构进行排序?
所以基本上,我试图根据C++ 基于多个条件对结构进行排序?,c++,sorting,lambda,C++,Sorting,Lambda,所以基本上,我试图根据int的值对struct Entry的向量进行排序(每个Entry都有一个string word和一个int count)。我通过内联lambda表达式实现了这一点: vector<Entry*> entries(old); //make copy of old vector std::stable_sort(entries.begin(), entries.end(), [] (const Entry *lhs, const Entry *rhs){
int
的值对struct Entry
的向量进行排序(每个Entry
都有一个string word
和一个int count
)。我通过内联lambda表达式实现了这一点:
vector<Entry*> entries(old); //make copy of old vector
std::stable_sort(entries.begin(), entries.end(), [] (const Entry *lhs, const Entry *rhs){
return (lhs->count > rhs->count);
});
矢量条目(旧)//复制旧向量
std::stable_sort(entries.begin()、entries.end()、[](常数项*lhs、常数项*rhs){
返回(左侧->计数>右侧->计数);
});
然而,我现在的问题是,如果两个或更多的
条目
具有相同的计数
,我需要按字母顺序对它们进行排序。是否可以在其中的某个地方使用另一个lambda表达式,或者是否有其他方法来实现这一点?谢谢 解决方案非常简单:
std::stable_sort(entries.begin(), entries.end(),
[] (const Entry *lhs, const Entry *rhs)
{
if( lhs->count != rhs->count )
return lhs->count > rhs->count;
else
return lhs->word > rhs->word;
});
重复的问题有点老了,并且错过了C++11的答案,但现在已经修复了。请随意在那里投票支持我的新答案;)