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, ...
随机推荐
- Solr.NET快速入门(八)【多核多实例,映射验证】
多核/多实例 本页介绍如何配置SolrNet访问(读/写)多个Solr内核或实例. 它假定您知道Solr内核是什么,如何在SolrNet外部配置和使用它们. 此页面不涵盖CoreAdminHandle ...
- VCRuntime静默安装
批处理脚本: "%~dp0\VC_X64Runtime\VC_x64Runtime.exe" /q"%~dp0\VC_X86Runtime\VC_X86Runtime.e ...
- apk回编译问题汇总(阶段)
反编译apk,在smali中注入一段自己的代码. 试了几个工具(apkdb.apktool.apkSign), 发现反编译都可以,但是回编译都不兼容java1.8,导致回编译成功,但apk没有签名,不 ...
- hdu2121 Ice_cream’s world II 最小树形图(难)
这题比HDU4009要难一些.做了4009,大概知道了最小树形图的解法.拿到这题,最直接的想法是暴力.n个点试过去,每个都拿来做一次根.最后WA了,估计是超时了.(很多题都是TLE说成WA,用了G++ ...
- 编写可维护的javascript阅读笔记
格式 变量 变量命名, 采取小驼峰大小写 变量使用名词, 函数前缀为动词 局部变量应统一定义在函数的最上面, 而不是散落在函数的任意角落. 赋初始值的定义在未赋初始值的变量的上面. 我个人建议不使用单 ...
- php进程控制
1 POSIX扩展 posix_access($file,$mode) 查看文件的访问权限,可以由is_readable等几个函数代替 posix_errno() 返回posix函数执 ...
- 原生node写一个静态资源服务器
myanywhere 用原生node做一个简易阉割版的anywhere静态资源服务器,以提升对node与http的理解. 相关知识 es6及es7语法 http的相关网络知识 响应头 缓存相关 压缩相 ...
- ZooKeeper概念
这可能是把ZooKeeper概念讲的最清楚的一篇文章 相信大家对 ZooKeeper 应该不算陌生,但是你真的了解 ZooKeeper 是什么吗?如果别人/面试官让你讲讲 ZooKeeper 是什么, ...
- Linux Shell脚本编程-语句控制
过程式编程语言bash脚本编程面向过程的编程 顺序执行:默认法则,按照顺序一条一条语句执行 选择执行:分支,条件判断,符合条件的分支予以执行 循环执行:将同一段代码反复执行有限次,所以循环必须有 ...
- python第四周:装饰器、迭代器、内置方法、数据序列化
1.装饰器 定义:本质是一个函数,(装饰其他函数)就是为其他函数添加附加功能 原则:不能修改被装饰函数的源代码,不能修改被装饰函数的调用方式 实现装饰器的知识储备: 函数即“变量”.每当定义一个函数时 ...