工作中有个需要使用sort对从mongo中查询到得数据进行排序,学习了下sort的一些用法。
#include <algorithm>
#include <vector>
bool comp(int d1, int d2)
{
return d1 >= d2;
}
int main()
{
std::vector<int> d(50,10);
std::sort(d.begin(), d.end(), comp);
return 0;
}
上面的代码看似没什么问题,但运行崩溃:
[root workspace]#./sort
*** Error in `./sort': double free or corruption (!prev): 0x00000000017f6010 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x81299)[0x7fc844b7f299]
./sort[0x4012d6]
./sort[0x400f6c]
./sort[0x400c9e]
./sort[0x400a6d]
./sort[0x40090f]
./sort[0x4007c9]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7fc844b20555]
./sort[0x400689]
======= Memory map: ========
00400000-00404000 r-xp 00000000 fd:00 5670555 /root/workspace/sort
00603000-00604000 r--p 00003000 fd:00 5670555 /root/workspace/sort
00604000-00605000 rw-p 00004000 fd:00 5670555 /root/workspace/sort
017f6000-01817000 rw-p 00000000 00:00 0 [heap]
7fc840000000-7fc840021000 rw-p 00000000 00:00 0
7fc840021000-7fc844000000 ---p 00000000 00:00 0
7fc844afe000-7fc844cc2000 r-xp 00000000 fd:00 33733216 /usr/lib64/libc-2.17.so
7fc844cc2000-7fc844ec1000 ---p 001c4000 fd:00 33733216 /usr/lib64/libc-2.17.so
7fc844ec1000-7fc844ec5000 r--p 001c3000 fd:00 33733216 /usr/lib64/libc-2.17.so
7fc844ec5000-7fc844ec7000 rw-p 001c7000 fd:00 33733216 /usr/lib64/libc-2.17.so
7fc844ec7000-7fc844ecc000 rw-p 00000000 00:00 0
7fc844ecc000-7fc844ee1000 r-xp 00000000 fd:00 34356254 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7fc844ee1000-7fc8450e0000 ---p 00015000 fd:00 34356254 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7fc8450e0000-7fc8450e1000 r--p 00014000 fd:00 34356254 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7fc8450e1000-7fc8450e2000 rw-p 00015000 fd:00 34356254 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7fc8450e2000-7fc8451e3000 r-xp 00000000 fd:00 33733224 /usr/lib64/libm-2.17.so
7fc8451e3000-7fc8453e2000 ---p 00101000 fd:00 33733224 /usr/lib64/libm-2.17.so
7fc8453e2000-7fc8453e3000 r--p 00100000 fd:00 33733224 /usr/lib64/libm-2.17.so
7fc8453e3000-7fc8453e4000 rw-p 00101000 fd:00 33733224 /usr/lib64/libm-2.17.so
7fc8453e4000-7fc8454cd000 r-xp 00000000 fd:00 33713580 /usr/lib64/libstdc++.so.6.0.19
7fc8454cd000-7fc8456cd000 ---p 000e9000 fd:00 33713580 /usr/lib64/libstdc++.so.6.0.19
7fc8456cd000-7fc8456d5000 r--p 000e9000 fd:00 33713580 /usr/lib64/libstdc++.so.6.0.19
7fc8456d5000-7fc8456d7000 rw-p 000f1000 fd:00 33713580 /usr/lib64/libstdc++.so.6.0.19
7fc8456d7000-7fc8456ec000 rw-p 00000000 00:00 0
7fc8456ec000-7fc84570e000 r-xp 00000000 fd:00 33733209 /usr/lib64/ld-2.17.so
7fc8458f1000-7fc8458f6000 rw-p 00000000 00:00 0
7fc84590b000-7fc84590d000 rw-p 00000000 00:00 0
7fc84590d000-7fc84590e000 r--p 00021000 fd:00 33733209 /usr/lib64/ld-2.17.so
7fc84590e000-7fc84590f000 rw-p 00022000 fd:00 33733209 /usr/lib64/ld-2.17.so
7fc84590f000-7fc845910000 rw-p 00000000 00:00 0
7ffc6f39b000-7ffc6f3bc000 rw-p 00000000 00:00 0 [stack]
7ffc6f3d9000-7ffc6f3db000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
已放弃(吐核)
在boost.org查询到comp函数主要有如下几个特性:
Irreflexivity f(x, x) must be false.
Antisymmetry f(x, y) implies !f(y, x)
Transitivity f(x, y) and f(y, z) imply f(x, z).
Transitivity of equivalence Equivalence (as defined above) is transitive: if x is equivalent to y and y is equivalent to z, then x is equivalent to z. (This implies that equivalence does in fact satisfy the mathematical definition of an equivalence relation.) [1]
我们上面的比较函数:
bool comp(int d1, int d2)
{
return d1 >= d2;
}
第一条f(x,x) == false就满足不了,由此会导致崩溃。至于为什么比较函数要设计成strict weak ordering,就需要深入stl源码一探究竟,等有时间研究下一本跟随我8 9年的书(但一直没看过)《stl源码剖析》。