返回信息流bool greater5(int value){return value>5};
vector<int> v1;
v1里面的内容是 1 2 3 4 5 6 7 8 9 10
然后random_shuffle(),得到 4 3 0 2 10 7 8 9 5 1 6
然后partition(v1.begin(),v1.end(),greater5)
得到的结果1是6 9 8 7 10 2 0 3 5 1 4.
按照我的理解得到的结果2应该是10 7 8 9 6 4 3 0 2 5 1
可是VC6.0运行的就是结果1.理解不了。求教大牛分析
这是一条镜像帖。来源:北邮人论坛 / cpp / #32399同步于 2009/11/30
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
大牛帮忙看看这个程序
lichehuo
2009/11/30镜像同步7 回复
订阅后,新回复会通过你的通知中心匿名送达。
7 条回复
能把代码贴全么。。。
【 在 lichehuo (忧郁门门主) 的大作中提到: 】
: bool greater5(int value){return value>5};
: vector<int> v1;
: v1里面的内容是 1 2 3 4 5 6 7 8 9 10
: ...................
【 在 DarkIce 的大作中提到: 】
: 能把代码贴全么。。。
代码是全的,都是STL的泛型函数。
应该是parttition的第三个参数greater5写的有问题
好吧,对这个函数不了解。。。
感觉可以debug跟一下就知道了
【 在 jokerlee (Jackal The Dire) 的大作中提到: 】
: 代码是全的,都是STL的泛型函数。
: 应该是parttition的第三个参数greater5写的有问题
【 在 jokerlee 的大作中提到: 】
: 代码是全的,都是STL的泛型函数。
: 应该是parttition的第三个参数greater5写的有问题
no, I don't think so.
I think result 1 is correct and explaination could be refered to "quick sort".
【 在 lichehuo 的大作中提到: 】
: bool greater5(int value){return value>5};
: vector<int> v1;
: v1里面的内容是 1 2 3 4 5 6 7 8 9 10
: ...................
suppose a int array of 10 elements, the pivot is set to array[0], the source code may looks like followings:
i = 1; j = 9;
while( i < j)
{
while( array[i] <= pivot && i < 10 ) ++i;
while( array[j] > pivot && j > 0 ) --j;
swap( array[i], array[j] );
}
swap( array[0], array[j] );
[em18]
【 在 epavel 的大作中提到: 】
: no, I don't think so.
: I think result 1 is correct and explaination could be refered to "quick sort".
谢谢指点,理解了
STL的partition就是快排里的partition, 让符合条件的元素在前,不符合条件的元素在后,MS采用的是双向扫描算法,没看代码,猜的
交换 4 6
4 3 0 2 10 7 8 9 5 1 6
p q
-------------------------------
交换 3 9
6 3 0 2 10 7 8 9 5 1 4
p q
-------------------------------
交换 0 8
6 9 0 2 10 7 8 3 5 1 4
p q
-------------------------------
交换 2 7
6 9 8 2 10 7 0 3 5 1 4
p q
-------------------------------
q<=p 结束
6 9 8 7 10 2 0 3 5 1 4
q p
-------------------------------