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, ...
随机推荐
- (转) 50个CSS技巧
这里我工作中收集了10个很不错的CSS技巧,你可以用在你的项目上.它可以帮你很好地整理你的元素并让他们看起来蛮酷的.下面开始我们的内容,希望你会喜欢它.下面是我收集的CSS技巧,希望能帮助到你,感觉收 ...
- Google浏览器“无法添加来自此网站的应用、扩展程序和应用脚本”的解决办法
原文链接:https://blog.csdn.net/Fan_Weibin/article/details/80402790 解决方法如下: 在桌面找到Google Chrome图标→右击属性→在快捷 ...
- 读《我是一只 IT 小小鸟》
读<我是一只 IT 小小鸟> 作为一个一向看重节操的体面人,即使面临许多 DDL 包括期中考试,在忙乱不堪的时候我也断不断告诫自己,不能迫于课程要求仅为了写出一篇笔记而去读书,以后更是如此 ...
- 【Oracle】搭建DG(DataGuard)
操作系统:OEL 5.6 Oracle 版本:11.2.0.4.0 DataGuard规划说明 DATABASE_ROLE DB_NAME IPADDR Primary lgr 192.168.10. ...
- [oracle] 递归追溯完整部门名称 函数
create or replace function fn_DeptWholeName2(objectid in number) return nvarchar2 is wholename nvarc ...
- APUE学习笔记3——文件和目录
1 简介 之前学习了执行I/O操作的基本函数,主要是围绕普通文件I/O的打开.读或写.下面继续学习Unix文件系统的其他特征和文件的基本性质.我们将从stat函数开始,了解stat结构所代表的文件属性 ...
- Kafka学习笔记(1)----Kafka的简介和Linux下单机安装
1. Kafka简介 Kafka is a distributed,partitioned,replicated commit logservice.它提供了类似于JMS的特性,但是在设计实现上完全不 ...
- web前端学习基础知识(1)
下载Atom插件和主题安装和配置 1.官网 https://atom.io/ 2.百度网盘上http://pan.baidu.com/s/1ntszCgT 安装subline以及插件的安装,再去了解它 ...
- win7不需要密码访问网络共享文件(转载)
大家是不是觉得,共享一个文件夹后,每次输入密码都很麻烦呢?有没有不需要输入密码就可以访问共享文件的方法呢? 答案是肯定的,当然有,下面介绍下win7不需要密码访问网络共享文件 工具/原料 两台电脑在局 ...
- Angular之constructor和ngOnInit差异及适用场景(转)
原始地址:https://blog.csdn.net/u010730126/article/details/64486997 Angular中根据适用场景定义了很多生命周期函数,其本质上是事件的响应函 ...