简单std::排序不起作用
Simple std::sort not working
我有以下代码:
int main()
{
int intArr[] = { 1,5,3 };
//auto f = [](auto a, auto b) {return a < b;};
//std::sort(intArr, intArr + 2, f);
std::sort(intArr, intArr + 2);
for (int& temp : intArr)
cout << temp << endl;
}
然而,输出是未排序的(例如,输出是1 5 3
)。将std::sort
与lambda一起使用时也会得到相同的结果。是什么导致了这种行为?
我使用的是Visual C++编译器(Visual Studio 2015)。
在采用范围的STL算法中,如果你想提供整个范围,你必须将一个元素的末尾作为结束,而不是范围本身的末尾,因此在你的情况下:
std::sort(intArr, intArr + 3);
或
std::sort(intArr, intArr + sizeof(intArr) / sizeof(int));
甚至更好:
std::sort(std::begin(intArr), std::end(intArr));
数组中有3个值,但只发送2个(因为在STL算法中,第二个参数是past-end iterator
)。应该是
std::sort(intArr, intArr + 3);
相关文章:
- 尝试使用C++中的模板进行BST排序,但在使用随机字符串进行测试时不起作用
- 我的C++合并排序代码不起作用。我在这里错过了什么?
- 使用选择排序对数组数据结构进行排序,但它不起作用
- c++ 选择排序在特定情况下不起作用
- 为什么当我们使用等于'='符号比较器函数时,c ++的内置排序函数不起作用?
- C++插入排序错误功能不起作用
- 我正在编写一个拆分为 3 的合并排序,我不知道为什么它不起作用
- 为什么我的替代排序算法不起作用?
- 堆排序它不起作用或计算不正确
- C++ ceil() 在傀儡排序中不起作用
- 合并两个排序的列表 - 不起作用
- 排序功能不起作用
- 气泡排序不传递数据或不起作用
- 外壳排序算法不起作用
- 排序不起作用 c++
- 我已经在 C++ 中实现了合并排序。它没有错误,但不知何故不起作用
- 在 C++ 中使用 10 个随机整数的数组进行气泡排序不起作用
- 合并排序在 C++ 中不起作用
- 合并排序wih pthreads在C 中不起作用
- 当中间元素选择为枢轴时,快速排序C 不起作用