STL 按变量排序,然后按升序排序
STL sort by variable and then in ascending order
如何按字母顺序排序,然后按类中的int
变量排序?如何组合它们,因此如果counter
相同,它将按字母顺序返回?
// sort pages by its popularity
bool popularity(const Page &a, const Page &b) {
return a.counter > b.counter;
}
// alphabetical sort
bool alphaSort(const Page &a, const Page &b) {
return a.name < b.name;
}
// Combination?
sort(.begin(), .end(), ??)
将两个标准扩展到词典比较:
bool combinedSort(const Page &a, const Page &b)
{
return a.counter > b.counter || (a.counter == b.counter && a.name < b.name);
}
使用上面的组合排序函数(修改了排序,因为您希望先按字母顺序排列,然后再按 int 值排列),您可以像这样调用排序:
假设您有一个页面向量
bool combinedSort(const Page &a, const Page &b)
{
return a.name < b.name || (a.name == b.name && a.counter < b.counter);
}
std::vector<Page> pages;
std::sort(pages.begin(), pages.end(), combinedSort);
相关文章:
- 按升序对链表进行排序
- 读取一组用户输入,按升序排序,然后打印结果
- 如何按升序对输入文件中的数字进行排序,并找到它们的范围和中值
- 数组将排序排序为降序而不是升序
- 编写程序,导入三个浮点数a,b和c.按升序排序
- 按升序对列表进行排序,C++使用气泡排序
- 按升序对数组进行排序嵌套循环问题
- 按特定值升序对字符串进行排序.我的实施中出了什么问题?
- C 气泡排序升序 - 不能对所有元素进行排序
- 偶数升序排序,奇数降序排序
- 按升序对矢量的相同元素进行排序
- C 排序升序非零值
- 重载比较符号"<"时,C++升序排序的正确方法
- 按升序对特征向量Xf进行排序
- 如何从文本文件中按升序对数据进行排序
- 如何按C 中的向量对升序和下降顺序进行排序
- C++按升序对链表进行排序
- 如何按距点的距离(x1,y1)升序对一组坐标进行排序
- C++在文本文件中按升序对数字进行排序
- c++排序(升序,降序)整型数组