C++ Primer 学习中。。。

简单记录下我的学习过程 (代码为主)

//全部容器适用

nth_element(b,n,e)

nth_element(b,n,e,p)

对照:partition()算法



/**------http://blog.csdn.net/u010579068------**/
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<list>
#include<functional>
#include<deque>
#include<algorithm>
using namespace std; /*****************************************
//全部容器适用
nth_element(b,n,e)
nth_element(b,n,e,p)
对照:partition()算法
*****************************************/
/**---------------------------------------------------------------------------------- ----------------------------------------------------------------------------------**/
/*************************************************************************************
std::nth_element 全部排序容器适用 algorithm
--------------------------------------------------------------------------------------
template <class RandomAccessIterator>
void nth_element ( RandomAccessIterator first, RandomAccessIterator nth,
RandomAccessIterator last ); template <class RandomAccessIterator, class Compare>
void nth_element ( RandomAccessIterator first, RandomAccessIterator nth,
RandomAccessIterator last, Compare comp );
//eg: *************************************************************************************/ bool myfunction (int i,int j)
{
return (i>j);
} int main ()
{
int m[]= {3,4,5,6,7,2,3,4,5,6,1,2,3,4,5};
vector<int> myvector(m,m+15);
vector<int>::iterator it; // set some values:
// for (int i=1; i<10; i++) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 // random_shuffle (myvector.begin(), myvector.end()); cout << "myvector contains:(原始数据)";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it; cout << endl;
// using default comparison (operator <):
// cout<<*(myvector.begin()+3)<<endl;
nth_element (myvector.begin(), myvector.begin()+3, myvector.end()); cout << "myvector contains:(从小到大)";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it; cout << endl; // using function as comp
// cout<<*(myvector.end()-4)<<endl;
// nth_element (myvector.begin(), myvector.begin()+3, myvector.end(),myfunction);
nth_element (myvector.begin(), myvector.end()-4, myvector.end(),greater<int>());
// print out content:
cout << "myvector contains:(从大到小)";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it; cout << endl; return 0;
}

STL_算法_依据第n个元素排序(nth_element)的更多相关文章

  1. cb51a_c++_STL_算法_根据第n个元素排序nth_element

    cb51a_c++_STL_算法_根据第n个元素排序nth_elementnth_element(b,n,e),比如最大的5个数排序,或者最小的几个数nth_element(b,n,e,p)对比:pa ...

  2. STL_算法_元素计数(count、count_if)

    C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) count . count_if #include<iostream> #include<cstdio&g ...

  3. STL_算法_对全部元素排序(sort、stable_sort)

    C++ Primer 学习中. . .   简单记录下我的学习过程 (代码为主) //大部分容器适用.不适用于list容器 sort(b,e) sort(b,e,p) stable_sort(b,e) ...

  4. STL_算法_中使用的函数对象

    写在前面: STL算法中的 函数对象的功能: (1).都是提供一种比较的函数,比较相邻的左右两个值的 相等/大小 等的关系, (2).返回值都是bool :该返回值 貌似是指明 遍历元素是否还要继续往 ...

  5. STL_算法_查找算法(lower_bound、upper_bound、equal_range)

    C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) //全部容器适用(O(log(n)))    已序区间查找算法 lower_bound()        //找第一个符合的 ...

  6. STL_算法_查找算法(find、find_if)

    C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) find . find_if /**********************线性查找O(n) find(); find_if ...

  7. STL_算法_逆转(reverse,reverse_copy)

    C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) //全部容器适用 reverse(b,e)        //逆转区间数据 reverse_copy(b,e,b2) /** ...

  8. STL_算法_查找算法(binary_search、includes)

    C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) 全部容器适用(O(log(n)))     已序区间查找算法 binary_search             //二分查 ...

  9. STL_算法_局部排序(partial_sort、partial_sort_copy)

    C++ Primer 学习中. . . 简单记录下我的学习过程 (代码为主) /***************************************** // partial_sort(b, ...

随机推荐

  1. MyBatis的关联映射和动态SQL

    CREATE TABLE tb_card ( id INT PRIMARY KEY AUTO_INCREMENT, CODE ) ); '); CREATE TABLE tb_person ( id ...

  2. 使用filezella服务器安装ftp

    使用FileZilla配置FTP站点,可参考以下步骤: 1.打开Filezilla Server服务端: 点击[Edit]->[Users],或者点击如下图标新增用户. 2.添加FTP帐号后,设 ...

  3. Map初始化

    转载至:http://blog.csdn.net/dujianxiong/article/details/54849079 第一种方法:static块初始化 public class Demo{ pr ...

  4. WinForm——操作word文档

    解决方案资源管理器——引用——(右击)添加引用——COM 1. 安装Office,添加引用COM里面的 Microsoft Word 14.0 Object. Library 2. 导命名空间 usi ...

  5. Unity3d 刚体

    using UnityEngine; using System.Collections; public class rigidbody_ : MonoBehaviour { private Rigid ...

  6. JavaScript实现网页换肤

    <html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...

  7. SQLServer 事务的隔离级别

    SQLServer事务的隔离级别 数据库是要被广大客户所共享访问的,那么在数据库操作过程中很可能出现以下几种不确定情况. 更新丢失(Lost update) 两个事务都同时更新一行数据,但是第二个事务 ...

  8. ReactiveX

    http://reactivex.io The real power comes with the “reactive extensions” (hence “ReactiveX”) — operat ...

  9. 前端开发—BOM对象DOM文档对象操作

    BOM 浏览器对象 BOM:Browser Object Model 操作浏览器,需要调用window对象,它是所有浏览器都支持的对象,表示的就是浏览器窗口 window对象可以通过点调用子对象 wi ...

  10. NYOJ-1013除法表达式

    除法表达式 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 给出一个这样的除法表达式:X1/X2/X3/···/Xk,其中Xi是正整数.除法表达式应当按照从左到右的顺 ...