相关疑难解决方法(0)

使用STL排序功能对列表进行排序

我正在尝试按降序排序列表(类的一部分)包含a的项目struct,但它不编译:

错误:'__last - __first'中'operator-'不匹配

sort(Result.poly.begin(), Result.poly.end(), SortDescending());
Run Code Online (Sandbox Code Playgroud)

这是SortDescending:

struct SortDescending
{
    bool operator()(const term& t1, const term& t2)
    { 
        return t2.pow < t1.pow; 
    }
};
Run Code Online (Sandbox Code Playgroud)

谁能告诉我什么是错的?

c++ sorting stl list

22
推荐指数
2
解决办法
2万
查看次数

为什么std :: list <int>不可排序?

我刚刚阅读了一篇关于C++概念的(印刷版,德语)文章(C++ 20).

本文使用以下Sortable概念给出了函数模板的示例:

template<typename Cont>
  requires Sortable<Cont>()
void sort(Cont& container) { ... }
Run Code Online (Sandbox Code Playgroud)

它声称编译器会拒绝以下代码:

std::list<int> lst = { 1, 7, 5, 12 };
sort(lst);
Run Code Online (Sandbox Code Playgroud)

有一个错误:

错误:lst不是带<的随机访问容器

假设文章是正确的-到底为什么是一个列表INT值不排序?对我来说,整数的列表就像是在考虑"可排序"的东西时的典型例子?!

不,我不是在询问std :: sort.我在问:为什么Sortable 概念不起作用std::list<int>

c++ list c++-concepts c++20

7
推荐指数
1
解决办法
373
查看次数

为什么只有std :: list :: sort()?

可能重复:
使用stl排序功能排序列表

C++标准库提供严格的线性序列容器,线性序列容器,关联容器.

std::sort()适用于各种容器.但为什么只提供列表排序.std::list::sort()

c++ sorting stl

3
推荐指数
2
解决办法
999
查看次数

标签 统计

c++ ×3

list ×2

sorting ×2

stl ×2

c++-concepts ×1

c++20 ×1