- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
在 C 中,排序通常如下例所示实现:
#include <stdio.h>
void Sort( int* arr, int n, bool(*cmp)(int,int) )
{
for( int i=0; i<n-1; i++ )
{
for( int j=i+1; j<n; j++ )
{
if( cmp(arr[i], arr[j]) )
swap( arr[i], arr[j] );
}
}
}
int ascending( int a, int b ) { return a > b; } // greater
int descending( int a, int b ) { return a < b; } // less
void main()
{
int arr[10] = { 1,3,5,7,9,2,4,6,8,10 };
// ascending
Sort( arr, 10, ascending );
for( int i=0; i<10; i++ )
printf( "%d ", arr[i] );
printf( "\n" );
// descending
Sort( arr, 10, descending );
for( int i=0; i<10; i++ )
printf( "%d ", arr[i] );
printf( "\n" );
}
所以我写了一些源代码,如下例所示,期望得到相同的结果:
#include <iostream>
#include <algorithm> // for sort
#include <functional> // for less & greater
using namespace std;
bool gt( int a, int b ) { return a > b; } // greater
bool ls( int a, int b ) { return a < b; } // less
void main()
{
int x[10] = { 1,3,5,7,9,2,4,6,8,10 };
// ascending but descending
sort( x, x+10, gt );
for( int i=0; i<10; i++ )
cout << x[i] << " ";
cout << endl;
// descending but ascending
sort( x, x+10, ls );
for( int i=0; i<10; i++ )
cout << x[i] << " ";
cout << endl;
greater<int> g; // a > b
less<int> l; // a < b
// ascending but descending
sort( x, x+10, g );
for( int i=0; i<10; i++ )
cout << x[i] << " ";
cout << endl;
// descending but ascending
sort( x, x+10, l );
for( int i=0; i<10; i++ )
cout << x[i] << " ";
cout << endl;
}
但我的预期并不正确。
为什么 STL 中的排序不像 C 中的排序那样工作?
最佳答案
std::sort
默认按升序排序。如果您正在寻找降序,这里是技巧:
int x[10] = { 1,3,5,7,9,2,4,6,8,10 };
std::vector<int> vec(x, x+10); // construct std::vector object
std::sort(vec.rbegin(),vec.rend()); // sort it in reverse manner
这样,您明确指出 std::sort
应该将您的数组视为其结尾即开头,反之亦然,这会导致您的数组按降序排序。 Here's the full example.
如果您想使用 std::less
和 std::greater
,那么它可能看起来像这样:
int x[10] = { 1,3,5,7,9,2,4,6,8,10 };
std::sort(x, x + 10, std::less<int>()); // for ascending order
std::sort(x, x + 10, std::greater<int>()); // for descending order
第二种解决方案的完整示例是 here .
关于c++ - 关于将 std::less 和 std::greater 与 std::sort 一起使用的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14828476/
我记得从 C 天起我们就被鼓励使用 i > -1 代替 i >= 0 因为性能。 这是否仍然适用于 C# .NET 世界?在当今的编译器中使用其中一种对性能有何影响?即编译器是否足够聪明,可以为您优化
比较小于/大于比小于/大于或等于计算性能更好吗? 凭直觉,人们可能会认为小于/大于稍微好一些。 编译器可以使用一些技巧来使比较看起来相同吗? 编译器可以消除例如小于或等于与小于通过将界限增加一来实现,
所以我想知道是否有一种方法可以实现双倍大于,如下所示: if(x > y > z) { ... } 然后我看到了这个 Expression for "more than x and less than
有些人写 std::nth_element(v.begin(), v.begin()+1, v.end(), std::greater{}); 还有一些是这样写的 std::nth_element(v
这个问题在这里已经有了答案: Speed of Comparison operators (6 个答案) 关闭 5 年前。 我有点好奇这些比较操作在幕后是如何工作的,因为我正在尝试尽可能地优化我的代
此代码有效: #include #include #include #include using namespace std; int main(){ priority_queue,g
我想显示以小时、分钟和秒为单位的时间长度,其中有些时间长度大于 24 小时。目前我正在尝试这个: $timeLength = new DateTime(); $timeLength->setTime(
在我看来,它们是一样的。但在 Visual Studio 2015 中,它们肯定是不同的。 //Ok, work properly multiset > ms1; ms1.insert(10); ms
假设我有两个符号 x,y=symbols('x y') 我的目的是告诉Sympy,x总是大于y(x> y)。有什么办法可以做到这一点? 最佳答案 无法直接执行此操作。 assumptions modu
这个问题在这里已经有了答案: Javascript string/integer comparisons (9 个回答) 关闭 6 年前。 在尝试确定一个值是否大于另一个值时,我遇到了一个奇怪的 J
我正在处理一个查询,我想在其中显示即将到来的日期的数量。即使日期大于当前日期,以下查询也会返回 0。请帮我解决这个问题。 SELECT (case when b.booked_date > cast
这是我的代码 #include #include #include #include using namespace std; /* struct greater {template
我有一个包含一百万个整数的数组,因为我正在试验并行快速排序。有时我有以下奇怪的行为: 为了检查数组是否排序正确,我在排序后输入了以下代码: for(int j=0; j array_parallel
template struct greater : binary_function { bool operator() (const T& x, const T& y) const {
我在使用基本 MySQL 查询时遇到了一个令人费解的问题。 这是我的 table : id | rating 1 | 1317.17 2 | 1280.59 3 | 995.12 4 | 97
我有如下数据集: table_a Product_Name Product_Orders game_296 1 game_298
我有几个疑问,其中大部分是: select * from Blah where col > 0 和 select * from Blah where date > current_date 由于它们都
我从列 A 的第 1 行输入了数值至IA .我想创建一个循环,将一个单元格与其之前的单元格进行比较(又名单元格 B1 到 A1 或单元格 F 到 E )。让我们使用 B1和 A1作为例子。它查看单元格
我知道 std::greater 是如何工作的。但是,当我阅读自 C++14 以来 std::greater 的 API 时,它的默认类型为 void。因此,如果我们不将任何模板参数传递给更大的它默认
有没有办法通过指定“日期大于 xxxxx”过滤器来返回 OData 中的一系列记录...但使用之前从 OData 源获取的日期? 用例:假设我想要构建一个网页来显示最近完成的在线订单的列表。这就是我的
我是一名优秀的程序员,十分优秀!