C++sort函数
之所以出现这个问题,这是因为stable_sort调用了别的一些函数,这种调用的参数包含了你vector中的元素,而在那些函数中,参数类型都是const的,所以最终predicate调用的时候,必须参数也是const的,举个简单的例子,相当于stable_sort干了以下的事情:
template
void stable_sort(Input begin, Input end, Pred p) {
do_something(*begin, p);
}
template
void do_something(const T &val, Pred p) {
p(val, val);
}
注意,在最后的do_something函数中,类型T就是你vector中元素的类型,从sort调用过来的时候,参数已经变成了const类型,所以pred调用的时候参数必须也是const的
如何使用深度优先搜索、广度优先搜索和迭代搜索算法来解决城市最短路径问题?
sort
n.
种类, 类别, 品种
品质, 本性, 性质
方法, 情形, 样子, 程度
某种人[物]
刷 一套铅字; [pl. ] 纺同级毛, 同型毛
[古](一)群, (一)伙
under all sorts of names
以各种各样的名义
people of every sort and kind
各种各样的人
They'll never stomach that sort of attitude.
他们永远不会容忍那样的态度。
He is the right sort.
他倒是挺合适的人。
He is my sort.
他正是我需要的人。
Queer sort (of a thing)this!
[口]这(东西)倒挺妙!
This copy is hard [runs] on sorts .
刷这件稿子要用几种铅字排。
习惯用语
after a sort
有些, 稍微, 有几分
in a sort
有些, 稍微, 有几分
all of a sort
差不多, 大同小异
all sort(s) of (=of all sorts)
各种各样的
all sorts and conditions of men(=men of all conditions)
各种各样的人, 各阶层的人
and that sort of thing
以及诸如此类的事情, 等等
a sort of
一种, 可以说是...的东西
He is not my sort.
[口]他不是我喜欢的那种人。
in any sort
无论如何, 必须
in a sort of way
略为, 比较
in some sort
多少, 稍微
in a sort
多少, 稍微
It takes all sorts (to make a world)
世上的人是形形色色[无奇不有]
no sort of
毫无[不]; 完全不是; 和...全不相称
nothing of the sort
根本没有那样的事情
of a sort
同一种[类], 相当的, 勉强称得上的, 较差的, 所谓的
of all sorts
各种各样的
of every sort and kind
各种各样的
of one's sort
和某人同样身分[性质, 品性]的
of sorts
=of a sort
各种各样的, 未经挑选的
of the sort
那样的; 这类的...., 诸如此类的...
out of sorts
觉得不舒服, 情绪不佳, 心里不自在; 刷铅字不全
some sort of
某种 的, 仿佛, 多少有些
that sort of thing
那[这]一类的事情
this sort of thing
那[这]一类的事情
That's your sort!
[口]那样做挺好!
那就是你的想法!
the better sort
[口]出色的人, 更高一级的人, 优秀的人
What sort of?
什么样的? 怎样的?
sort ill with sth.
同某事物不相称, 同某物不协调
sort of
[作状语用]有几分地; 在一定程度上
kind of
[作状语用]有几分地; 在一定程度上
sort out
整理好
挑出
解决
整顿
惩罚, 打败
[和反身代词连用]解决...之间的问题, (使)恢复正常
sort out from
把...从...挑出来
sort well with sth.
同某物相称, 同某物协调
若需对vector, string, deque, 或 array容器进行全排序,你可选择sort或stable_sort;
若只需对vector, string, deque, 或 array容器中取得top n的元素,部分排序partial_sort是首选.
若对于vector, string, deque, 或array容器,你需要找到第n个位置的元素或者你需要得到top n且不关系top n中的内部顺序,nth_element是最理想的;
若你需要从标准序列容器或者array中把满足某个条件或者不满足某个条件的元素分开,你最好使用partition或stable_partition;
若使用的list容器,你可以直接使用partition和stable_partition算法,你可以使用list::sort代替sort和stable_sort排序。若你需要得到partial_sort或nth_element的排序效果,你必须间接使用。正如上面介绍的有几种方式可以选择。
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!