按降序对向量进行排序
我应该使用
std::sort(numbers.begin(), numbers.end(), std::greater<int>());
或
std::sort(numbers.rbegin(), numbers.rend()); // note: reverse iterators
按降序对向量进行排序吗?一种方法或另一种方法有什么优点或缺点吗?
Should I use
std::sort(numbers.begin(), numbers.end(), std::greater<int>());
or
std::sort(numbers.rbegin(), numbers.rend()); // note: reverse iterators
to sort a vector in descending order? Are there any benefits or drawbacks with one approach or the other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
使用 c++14 你可以这样做:
With c++14 you can do this:
使用第一个:
它清楚地表明了正在发生的事情 - 即使有注释,将
rbegin
误读为begin
的可能性也较小。它清晰易读,这正是您想要的。此外,考虑到反向迭代器的性质,第二个可能比第一个效率低,尽管您必须对其进行分析才能确定。
Use the first:
It's explicit of what's going on - less chance of misreading
rbegin
asbegin
, even with a comment. It's clear and readable which is exactly what you want.Also, the second one may be less efficient than the first given the nature of reverse iterators, although you would have to profile it to be sure.
这又如何呢?
What about this?
您可以使用 Lambda 函数,而不是 Mehrdad 提出的函子。
Instead of a functor as Mehrdad proposed, you could use a Lambda function.
根据我的机器,使用第一种方法对 [1..3000000] 的
long long
向量进行排序大约需要 4 秒,而使用第二种方法大约需要两倍的时间。显然,这说明了一些问题,但我也不明白为什么。只是认为这会有所帮助。此处报告了同样的情况。
正如 Xeo 所说,使用
-O3
他们大约使用相同的时间来完成。According to my machine, sorting a
long long
vector of [1..3000000] using the first method takes around 4 seconds, while using the second takes about twice the time. That says something, obviously, but I don't understand why either. Just think this would be helpful.Same thing reported here.
As said by Xeo, with
-O3
they use about the same time to finish.TL;DR
使用任何。它们几乎是一样的。
无聊的答案
像往常一样,有利有弊。
使用
std::reverse_iterator
:operator>()
std::greater()
时使用
std::greater
时时 至于性能,两种方法同样高效。我尝试了以下基准测试:
使用此命令行:
std::greater演示
std::reverse_iterator
演示时间是一样的。 Valgrind 报告了相同数量的缓存未命中。
TL;DR
Use any. They are almost the same.
Boring answer
As usual, there are pros and cons.
Use
std::reverse_iterator
:operator>()
std::greater<int>()
Use
std::greater
when:As for performance, both methods are equally efficient. I tried the following benchmark:
With this command line:
std::greater
demostd::reverse_iterator
demoTimings are same. Valgrind reports the same number of cache misses.
您可以使用第一种方法,因为它比第二种方法效率更高。
第一种方法的时间复杂度低于第二种方法。
You may use the first approach because of getting more efficiency than second.
The first approach's time complexity less than second one.
您可以使用第一个或尝试下面的代码,它同样有效
You can either use the first one or try the code below which is equally efficient
我认为您不应该使用问题中的任何一种方法,因为它们都很令人困惑,而第二种方法正如 Mehrdad 所建议的那样脆弱。
我提倡以下内容,因为它看起来像标准库函数并且其意图很明确:
I don't think you should use either of the methods in the question as they're both confusing, and the second one is fragile as Mehrdad suggests.
I would advocate the following, as it looks like a standard library function and makes its intention clear:
对于 C++20:
这排除了传入无效迭代器对的任何可能性,例如:
For C++20:
This rules out any possibility of passing in an invalid pair of iterators like: