将指针转换为 STL 中的反向向量迭代器
我有
sort(arr, arr+n, pred);
如何按相反顺序排序?
I have
sort(arr, arr+n, pred);
How do I sort in reverse order?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我有
sort(arr, arr+n, pred);
如何按相反顺序排序?
I have
sort(arr, arr+n, pred);
How do I sort in reverse order?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
似乎也有可能使用反向迭代器......除了使用反向谓词可能更容易,除非类型没有实现
operator>
:)There also seems to be a possibility to use reverse iterators ... except using the reversed predicate might be easier, except perhaps when the type doesn't implement
operator>
:)如果你得到了
pred
(即你无法进入其中来反转顺序),类似于:If you're given
pred
(i.e. you can't get inside it to reverse the order), something like:您可以使用标准库中的
greater
,它会自动为您要排序的类型调用operator>
。You could use
greater
from the standard library which callsoperator>
automatically for the type you want to sort.对
pred
的返回值求反。Negate the return value of
pred
.正如阿尔拉迪所说,你应该提供一个反向谓词。如果由于某些原因(例如纯粹的懒惰)而不能,您总是可以先排序然后反转:
这对计算机来说会做更多的工作,但它很清楚并且可以完成工作。如果您需要这种排序的速度性能,请使用反向谓词解决方案。
As alrady said, you should provde a reversed predicate. If you can't for some reasons (like pure laziness), you can always first sort then reverse :
That would be more work for the computer but it's clear and does the job. If you need speed performance for this sort, use the reversed predicate solution.
我看起来很简单
Quite Easy i seems
请参阅:http://www.cplusplus.com/reference/std/function/not1 /
See: http://www.cplusplus.com/reference/std/functional/not1/