sort()和stable_sort()的比较器的要求之间的差异
Difference between the requirement of a comparator in sort() and stable_sort()
如果我通过lambda作为参考参数来对其工作进行排序。但是,当我将同一lambda传递给stable_sort
时,它没有编译,我必须声明参数const
?为什么是这样?
// sort algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::sort
#include <vector> // std::vector
using namespace std;
void print(std::vector<int> &vec)
{
for (auto i : vec)
{
cout << i << " ";
}
cout << endl;
}
int main() {
std::vector<int> myvector = { 32, 71, 12, 45, 26, 80, 53, 33 };
sort(myvector.begin(), myvector.end(), [](int &i, int &j)//working
{
return i < j;
});
stable_sort(myvector.begin(), myvector.end(), [](int &i, int &j)//not compiling
{
return i < j;
});
stable_sort(myvector.begin(), myvector.end(), [](const int &i, const int &j)//compiling
{
return i < j;
});
print(myvector);
return 0;
}
[BUG LIBSTDC /82891] Stestable_sort()不会与函数对象一起编译,该函数对象通过non-const Reference
进行参数现在有一个LWG问题提交请求等待发布 这。添加后我会返回。
GCC和LLVM都有问题,但他们等待请求 [3] 到图书馆工作组更改标准。
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- 为什么 std::unique 不调用 std::sort?
- 在cuda线程之间共享大量常量数据
- 在c代码之间共享数据的最佳方式
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- 大小相等但成员数量不同的结构之间的性能差异
- 类与私有变量的其他类之间的线程安全性
- 如何在cpp文件之间切换窗口?在Qt中
- 线程之间的布尔停止信号
- 对字符串进行排序时,在c++中处理sort()
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 在 const 函数中通过引用和指针返回之间的区别
- 我想知道长双倍和双倍之间的区别
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 如何使 std::sort 在 std::swap 和我的命名空间的模板化交换之间没有名称冲突?
- sort()和stable_sort()的比较器的要求之间的差异