cb51a_c++_STL_算法_根据第n个元素排序nth_element
cb51a_c++_STL_算法_根据第n个元素排序nth_element
nth_element(b,n,e),比如最大的5个数排序,或者最小的几个数
nth_element(b,n,e,p)
对比:partition()算法,分区算法
error C2675: 一元“++”:“TT88”不定义该运算符或到预定义运算符可接收类型的转换
for (TT88::iterator iter = ideq.begin(); iter != ideq.end(); ++ideq),写错了,应该是++iter
/*cb51a_c++_STL_算法_根据第n个元素排序nth_element
nth_element(b,n,e),比如最大的5个数排序,或者最小的几个数
nth_element(b,n,e,p)
对比:partition()算法,分区算法 error C2675: 一元“++”:“TT88”不定义该运算符或到预定义运算符可接收类型的转换
for (TT88::iterator iter = ideq.begin(); iter != ideq.end(); ++ideq),写错了,应该是++iter
*/ #include <iostream>
#include <algorithm>
#include <deque>
#include <iterator>
#include <functional> using namespace std; template <class TT88>
void print88(TT88 &ideq)
{
for (TT88::iterator iter = ideq.begin(); iter != ideq.end(); ++iter)
cout << *iter << ' ';
cout << endl;
} int main()
{
deque<int> ideq,ideq2;
for (int i = ; i <= ; ++i)
ideq.push_back(i);
for (int i = ; i <= ; ++i)
ideq.push_back(i);
for (int i = ; i <= ; ++i)
ideq.push_back(i);
ideq2 = ideq; print88(ideq);
nth_element(ideq.begin(), ideq.begin() + , ideq.end());//默认less<int>(),升序
cout << "nth_element排序后:" << endl;
print88(ideq); cout << "用copy,到输出流迭代器显示:" << endl;
copy(ideq.begin(), ideq.begin() + , ostream_iterator<int>(cout, " "));
cout << endl; ideq = ideq2;
nth_element(ideq.begin(), ideq.begin() + , ideq.end(),greater<int>());//降序,大到小
cout << "降序:" << endl;
print88(ideq); deque<int>::iterator pos;
//pos = partition(ideq.begin(), ideq.end(), bind2nd(greater<int>(), 3));
cout << endl;
ideq = ideq2;
cout << "分区partition小于等于3放左边,其余放右边" << endl;
pos = partition(ideq.begin(), ideq.end(), bind2nd(less_equal<int>(), ));
print88(ideq);
copy(ideq.begin(), pos, ostream_iterator<int>(cout, " "));
return ;
}
cb51a_c++_STL_算法_根据第n个元素排序nth_element的更多相关文章
- STL_算法_依据第n个元素排序(nth_element)
C++ Primer 学习中... 简单记录下我的学习过程 (代码为主) //全部容器适用 nth_element(b,n,e) nth_element(b,n,e,p) 对照:partition() ...
- cb50a_c++_STL_算法_局部排序partial_sort
cb50a_c++_STL_算法_局部排序partial_sort partial_sort(b,se,e)排序一部分,begin,source end,endcout << " ...
- cb49a_c++_STL_算法_对所有元素排序_sort_stable_sort
cb49a_c++_STL_算法_对所有元素排序_sort_stable_sort sort(b,e) sort(b,e,p) stable_sort(b,e) stable_sort(b,e,p) ...
- cb48a_c++_STL_算法_重排和分区random_shuffle_stable_partition
cb48a_c++_STL_算法_重排和分区random_shuffle_stable_partition random_shuffle()//重排,随机重排,打乱顺序 partition()分区,把 ...
- cb47a_c++_STL_算法_排列组合next_prev_permutation
cb47a_c++_STL_算法_排列组合next_prev_permutation 使用前必须先排序.必须是 1,2,3或者3,2,1.否者结果不准确.如果, 1,2,4,6.这样数据不会准确nex ...
- cb46a_c++_STL_算法_逆转和旋转reverse_rotate函数advance
cb46a_c++_STL_算法_逆转和旋转reverse_rotateSTL算法--变序性算法reverse() 逆转reverse_copy()一边复制一般逆转rotate()旋转,某个位置开始前 ...
- cb45a_c++_STL_算法_删除_(3)_unique(唯一的意思)删除连续性的重复的数据
cb45a_c++_STL_算法_删除_(3)_unique(唯一的意思)删除连续性的重复的数据unique(b,e),删除连续性的,删除重复的数据,比如如果有两个连续的5,5,则留下一个.uniqu ...
- cb44a_c++_STL_算法_删除_(2)remove_copy_remove_copy_if
cb44a_c++_STL_算法_删除_(2)remove_copy_remove_copy_if remove_copy()//在复制过程中删除一些数据remove_copy_if() 删除性算法: ...
- cb43a_c++_STL_算法_删除_(1)remove_remove_if
cb43a_c++_STL_算法_删除_(1)remove_remove_ifremove()remove_if() 注意:1.并不是真正的删除,而是把后面的元素向前移动,覆盖被删除元素,元素个数并没 ...
随机推荐
- Kubernetes Dashborad 搭建
需求 基于网页查看Kubernetes 用户管理界面 安装步骤 在控制面板节点部署dashborad kubectl apply -f https://raw.githubusercontent.co ...
- linux静态网络设置
一:NET模式 第一种: 第二种方式: 三:重启服务
- MySQL知识-MySQL不同版本多实例
一.不同版本多实例 0.软连接不同版本软件,修改环境变量 [root@db01 database]# ln -s mysql-5.6.46-linux-glibc2.12-x86_64 mysql ...
- [Android应用开发] 03.网络编程
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- [转]从 Apple TV 看电视的进化
电视被许多人吐槽为 “几十年没变过的东西”,因此苹果也被寄予厚望能改变这件事物.可惜的是,这种期望在空中飘了这么久,苹果也没玩出多少花样,直到这次发布会 Apple TV 才有了一些值得期待的改进. ...
- 安装xlrd包的时候,总是报错:ERROR: Could not install packages due to an EnvironmentError: HTTPConnectionPool (host='127.0.0.1', port=8888):。。。
安装xlrd包的时候,总是报错:ERROR: Could not install packages due to an EnvironmentError: HTTPConnectionPool (ho ...
- Java实现 LeetCode 733 图像渲染(DFS)
733. 图像渲染 有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 到 65535 之间. 给你一个坐标 (sr, sc) 表示图像渲染开始的像素值(行 ,列)和一个新的 ...
- Java实现 LeetCode 709 转换成小写字母(ASCII码处理)
709. 转换成小写字母 实现函数 ToLowerCase(),该函数接收一个字符串参数 str,并将该字符串中的大写字母转换成小写字母,之后返回新的字符串. 示例 1: 输入: "Hell ...
- Java实现 LeetCode 655 输出二叉树(DFS+二分)
655. 输出二叉树 在一个 m*n 的二维字符串数组中输出二叉树,并遵守以下规则: 行数 m 应当等于给定二叉树的高度. 列数 n 应当总是奇数. 根节点的值(以字符串格式给出)应当放在可放置的第一 ...
- Java实现 蓝桥杯VIP 算法训练 暗恋
算法训练 暗恋 时间限制:1.0s 内存限制:256.0MB 问题描述 同在一个高中,他却不敢去找她,虽然在别人看来,那是再简单不过的事.暗恋,是他唯一能做的事.他只能在每天课间操的时候,望望她的位置 ...