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. java.sql.SQLException: Field 'id' doesn't have a default value解决方案

    转自:https://blog.csdn.net/xinghuo0007/article/details/51810867 自增长:java.sql.SQLException: Field 'id' ...

  2. 关于Spring中的<context:annotation-config/>配置作用

    转自:https://www.cnblogs.com/iuranus/archive/2012/07/19/2599084.html 当我们需要使用BeanPostProcessor时,直接在Spri ...

  3. 用户注册登录验证 多版本集合 + hashlib加密

    #!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2018/5/6 0006 12:22# @Author : Anthony.Waa# @S ...

  4. 【Oracle】创建概要文件

    任务1:创建profile 创建概要文件my_profile 1)密码复杂性要求:启用: 2)密码长度最小值:8位: 3)密码错误输入三次,锁定账户,2分钟后自动解锁 --创建密码复杂度校验函数 @? ...

  5. ES : 软件工程学的复杂度理论及物理学解释

    系统论里面总是有一些通用的专业术语 比如复杂度.熵.焓,复杂度专门独立出来,成为复杂度理论 文章摘抄于:<非线性动力学> 刘秉政 编著  5.5 复杂性及其测度 热力学的几个专业术语 熵. ...

  6. PhotoZoom放大图片,真的能无损吗?

    当然想要无损放大一张很小的图片时,总会有人会和你推荐PhotoZoom这款软件,那么它真的和说的一样,可以无损放大吗?下面小编挑了2张图片做了一下对比. 案例1,我们选取一张尺寸为200x200像素的 ...

  7. 这里介绍两种将矩阵写入TXT文件的方法。

    方法1 fid = fopen('data.txt','wt'); % data.txt为写入文件名 matrix = M; % M为要存储的矩阵 [m,n]=size(matrix); for i= ...

  8. linux 性能分析与优化

    一.影响Linux服务器性能的因素 1.操作系统级 (CPU 内存 磁盘I/O性能 网络带宽) 2.程序应用级 二.系统性能评估标准   好  坏 极差 cpu user% +sys% <70% ...

  9. 洛谷P3807 【模板】卢卡斯定理_组合数学模板

    Code: #include<cstdio> using namespace std; typedef long long LL; const int maxn=1000000+2; LL ...

  10. [网络流24题] 最长k可重区间集问题 (费用流)

    洛谷传送门 LOJ传送门 很巧妙的建图啊...刚了$1h$也没想出来,最后看的题解 发现这道题并不类似于我们平时做的网络流题,它是在序列上的,且很难建出来二分图的形. 那就让它在序列上待着吧= = 对 ...