STL_算法_依据第n个元素排序(nth_element)
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)的更多相关文章
- cb51a_c++_STL_算法_根据第n个元素排序nth_element
cb51a_c++_STL_算法_根据第n个元素排序nth_elementnth_element(b,n,e),比如最大的5个数排序,或者最小的几个数nth_element(b,n,e,p)对比:pa ...
- STL_算法_元素计数(count、count_if)
C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) count . count_if #include<iostream> #include<cstdio&g ...
- STL_算法_对全部元素排序(sort、stable_sort)
C++ Primer 学习中. . . 简单记录下我的学习过程 (代码为主) //大部分容器适用.不适用于list容器 sort(b,e) sort(b,e,p) stable_sort(b,e) ...
- STL_算法_中使用的函数对象
写在前面: STL算法中的 函数对象的功能: (1).都是提供一种比较的函数,比较相邻的左右两个值的 相等/大小 等的关系, (2).返回值都是bool :该返回值 貌似是指明 遍历元素是否还要继续往 ...
- STL_算法_查找算法(lower_bound、upper_bound、equal_range)
C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) //全部容器适用(O(log(n))) 已序区间查找算法 lower_bound() //找第一个符合的 ...
- STL_算法_查找算法(find、find_if)
C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) find . find_if /**********************线性查找O(n) find(); find_if ...
- STL_算法_逆转(reverse,reverse_copy)
C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) //全部容器适用 reverse(b,e) //逆转区间数据 reverse_copy(b,e,b2) /** ...
- STL_算法_查找算法(binary_search、includes)
C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) 全部容器适用(O(log(n))) 已序区间查找算法 binary_search //二分查 ...
- STL_算法_局部排序(partial_sort、partial_sort_copy)
C++ Primer 学习中. . . 简单记录下我的学习过程 (代码为主) /***************************************** // partial_sort(b, ...
随机推荐
- 排序系列 之 希尔排序算法 —— Java实现
基本思想: 希尔排序的实质就是分组插入排序,又称缩小增量法. 将整个无序序列分割成若干个子序列(由相隔某个“增量”的元素组成的)分别进行直接插入排序,然后依次缩减增量再进行排序,待整个序列中的元素基本 ...
- js设计模式-门面模式
适用场景:门面模式在DOM脚本编程这种需要对各种不一致的浏览器接口的环境中很常用. 例子:阻止模式事件 var DED = widow.DED || {}; DED.util = { stopProp ...
- computed与methods的异同
在vue.js中,有methods和computed两种方式来动态当作方法来用的 如下: 两种方式在这种情况下的结果是一样的 写法上的区别是computed计算属性的方式在用属性时不用加(),而met ...
- mod_wsgi 初体验
1, 安装 ./configure --with-apxs=/usr/local/apache2/bin/apxs --with-python=/usr/bin/python3 make && ...
- springboot整合ActiveMQ 2(主备模式,负载均衡)
基本使用,https://www.tapme.top/blog/detail/2018-09-05-10-38 主备模式,https://www.tapme.top/blog/detail/2018- ...
- java8-3-LambdaMapReduce例子
public class LambdaMapReduce { private static List<User> users = Arrays.asList( new User(1, &q ...
- oracle11g安装与拆卸
Oracle 11g安装 1.解压下载的包,然后进入包内,点击setup.exe开始安装 . 2.出现如下:一般把那个小对勾取消,点击下一步进行, 弹出下图这个后点'是' 3.下图后,选择创建和配置数 ...
- vue-阻止事件冒泡-开启右键-键盘类事件
一: 阻止事件冒泡 布局: 当点击按钮时,会触发button的click 也会触发父级的方法 <div id="box"> <div @click="p ...
- 用来生成get set string 方法
https://projectlombok.org/ 主要是用来生成get set string 方法等等 原理是注解
- Docker的特性解析
Docker简介与入门:http://segmentfault.com/blog/p_chou/1190000000448808 Docker是个新生的事物,概念类似虚拟化.网上关于Docker入门的 ...