| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2117 人关注过本帖
标题:STL: nth_element为什么结果和sort一样?
取消只看楼主 加入收藏
hwdwow
Rank: 2
等 级:论坛游民
帖 子:119
专家分:98
注 册:2009-3-21
结帖率:72%
收藏
已结贴  问题点数:20 回复次数:1 
STL: nth_element为什么结果和sort一样?
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <time.h>

using namespace std;

int Random(int low=0, int high=RAND_MAX)
{
    return rand()%(high-low+1)+low;
}


bool exam(int x)
{
    return x>=60;
}
   
int main()
{
    int i;
    vector<int> a;
    vector<int> b;
    srand((unsigned)time(0));
    for (i=0; i<10; i++)
        a.push_back(Random(1,100));
    copy(a.begin(),a.end(),ostream_iterator<int>(cout,","));
    cout<< endl;

    b=a;
    sort(b.begin(), b.end());
    cout<< "sort: ";
    copy(b.begin(),b.end(),ostream_iterator<int>(cout,","));
    cout<< endl;

    b=a;
    sort(b.begin(), b.end(), greater<int>());
    cout<< "sort(greater): ";
    copy(b.begin(),b.end(),ostream_iterator<int>(cout,","));
    cout<< endl;

    b=a;
    nth_element(b.begin(), b.begin()+3, b.end());    // 为什么结果和sort一样?
    cout<< "nth_element: ";
    copy(b.begin(),b.end(),ostream_iterator<int>(cout,","));
    cout<< endl;

    b=a;
    partial_sort(b.begin(), b.begin()+3, b.end());
    cout<< "partial_sort: ";
    copy(b.begin(),b.end(),ostream_iterator<int>(cout,","));
    cout<< endl;

    vector<int> c(3);
    b=a;
    partial_sort_copy(b.begin(), b.end(), c.begin(), c.end());
    cout<< "partial_sort_copy: ";
    copy(c.begin(),c.end(),ostream_iterator<int>(cout,","));
    cout<<"   b: ";
    copy(b.begin(),b.end(),ostream_iterator<int>(cout,","));
    cout<< endl;

    b=a;
    partition(b.begin(), b.end(), exam);
    cout<< "partition: ";
    copy(b.begin(),b.end(),ostream_iterator<int>(cout,","));
    cout<< endl;
   
    return 0;
}
搜索更多相关主题的帖子: 结果 nth sort element STL 
2009-10-08 07:41
hwdwow
Rank: 2
等 级:论坛游民
帖 子:119
专家分:98
注 册:2009-3-21
收藏
得分:0 
4,70,49,62,32,32,83,38,2,72,
sort: 2,4,32,32,38,49,62,70,72,83,
sort(greater): 83,72,70,62,49,38,32,32,4,2,
nth_element: 2,4,32,32,38,49,62,70,72,83,     // 一样啊,郁闷
partial_sort: 2,4,32,70,62,49,83,38,32,72,
partial_sort_copy: 2,4,32,   b: 4,70,49,62,32,32,83,38,2,72,
partition: 72,70,83,62,32,32,49,38,2,4,
list::sort:  2,4,32,32,38,49,62,70,72,83,
Press any key to continue
2009-10-11 09:51
快速回复:STL: nth_element为什么结果和sort一样?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012482 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved