根据多种条件对结构进行分类
Sorting structs based on multiple conditions?
基本上,我正在尝试按int
s的值对struct Entry
的向量(每个Entry
都有string word
和int count
)。我设法通过inline 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);
});
但是,我现在遇到的问题是,如果两个或多个Entry
S具有相同的count
,则我需要按字母顺序排序。是否可以在其中某个地方使用另一个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# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 无法将结构注册为增强几何体3D点
- 多成员Constexpr结构初始化
- C++将文本文件中的数据读取到结构数组中
- 如何重构类层次结构以避免菱形问题
- 如何在C++中序列化结构数据
- std::vector的包装器,使数组的结构看起来像结构的数组
- 如何读取文件并将该文件分类为不同类型的数据,这些数据都存储在结构中的特定数组中
- 如何在类 c++ 中对数据结构进行分类
- 根据没有类或结构的另一个向量的元素对一个向量进行分类
- 使用C 中的SVM结构多类分类
- 根据多种条件对结构进行分类