STL_算法_对全部元素排序(sort、stable_sort)
C++ Primer 学习中。
。
。
简单记录下我的学习过程 (代码为主)
//大部分容器适用、不适用于list容器
sort(b,e)
sort(b,e,p)
stable_sort(b,e)
stable_sort(b,e,p)
/**------http://blog.csdn.net/u010579068------**/
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<algorithm>
using namespace std; /*****************************************
//大部分容器适用、不适用于list容器
sort(b,e)
sort(b,e,p)
stable_sort(b,e)
stable_sort(b,e,p)
*****************************************/
/**----------------------------------------------------------------------------------
注意:不适用于list容器,list有成员函数sort()
----------------------------------------------------------------------------------**/
/*************************************************************************************
std::sort 全部排序容器适用 algorithm
--------------------------------------------------------------------------------------
template <class RandomAccessIterator>
void sort ( RandomAccessIterator first, RandomAccessIterator last ); template <class RandomAccessIterator, class Compare>
void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );
//eg: *************************************************************************************/ /*************************************************************************************
std::stable_sort 全部排序容器适用 algorithm
--------------------------------------------------------------------------------------
template <class RandomAccessIterator>
void stable_sort ( RandomAccessIterator first, RandomAccessIterator last ); template <class RandomAccessIterator, class Compare>
void stable_sort ( RandomAccessIterator first, RandomAccessIterator last,
Compare comp );
//eg: *************************************************************************************/ bool myfunction (int i,int j)
{
return (i<j);
} struct myclass
{
bool operator() (int i,int j)
{
return (i<j);
}
} myobject;
bool compare_as_ints (double i,double j)
{
return (int(i)<int(j));
} int main ()
{
int myints[] = {32,71,12,45,26,80,53,33};
vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33
vector<int>::iterator it; // using default comparison (operator <):
sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33 // using function as comp
sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80) // using object as comp
sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80) // print out content:
cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it; cout << endl;
/**---------------------------------------------------------------------------------------**/ double mydoubles[] = {3.14, 1.41, 2.72, 4.67, 1.73, 1.32, 1.62, 2.58}; deque<double> mydeque;
deque<double>::iterator id; mydeque.assign(mydoubles,mydoubles+8); cout << "using default comparison:";
stable_sort (mydeque.begin(), mydeque.end());
for (id=mydeque.begin(); id!=mydeque.end(); ++id)
cout << " " << *id; mydeque.assign(mydoubles,mydoubles+8); cout << "\nusing 'compare_as_ints' :";
stable_sort (mydeque.begin(), mydeque.end(), compare_as_ints);
for (id=mydeque.begin(); id!=mydeque.end(); ++id)
cout << " " << *id; cout << endl; return 0;
}
myvector contains: 12 26 32 33 45 53 71 80
using default comparison: 1.32 1.41 1.62 1.73 2.58 2.72 3.14 4.67
using 'compare_as_ints' : 1.41 1.73 1.32 1.62 2.72 2.58 3.14 4.67
STL_算法_对全部元素排序(sort、stable_sort)的更多相关文章
- 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) ...
- 《Algorithms算法》笔记:元素排序(4)——凸包问题
<Algorithms算法>笔记:元素排序(4)——凸包问题 Algorithms算法笔记元素排序4凸包问题 凸包问题 凸包问题的应用 凸包的几何性质 Graham 扫描算法 代码 凸包问 ...
- 《Algorithms算法》笔记:元素排序(3)——洗牌算法
<Algorithms算法>笔记:元素排序(3)——洗牌算法 Algorithms算法笔记元素排序3洗牌算法 洗牌算法 排序洗牌 Knuth洗牌 Knuth洗牌代码 洗牌算法 洗牌的思想很 ...
- 《Algorithm算法》笔记:元素排序(2)——希尔排序
<Algorithm算法>笔记:元素排序(2)——希尔排序 Algorithm算法笔记元素排序2希尔排序 希尔排序思想 为什么是插入排序 h的确定方法 希尔排序的特点 代码 有关排序的介绍 ...
- 《Algorithms算法》笔记:元素排序(1)——简单排序
<Algorithms算法>元素排序(1)——简单排序 Algorithms算法元素排序1简单排序 排序问题 1 回调函数 2Java中回调函数的路线图 3 全序 4 Comparable ...
- STL_算法_依据第n个元素排序(nth_element)
C++ Primer 学习中... 简单记录下我的学习过程 (代码为主) //全部容器适用 nth_element(b,n,e) nth_element(b,n,e,p) 对照:partition() ...
- STL_算法_局部排序(partial_sort、partial_sort_copy)
C++ Primer 学习中. . . 简单记录下我的学习过程 (代码为主) /***************************************** // partial_sort(b, ...
- STL_算法_元素计数(count、count_if)
C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) count . count_if #include<iostream> #include<cstdio&g ...
- STL_算法_查找算法(lower_bound、upper_bound、equal_range)
C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) //全部容器适用(O(log(n))) 已序区间查找算法 lower_bound() //找第一个符合的 ...
随机推荐
- Android通过泛型简化findViewById类型转换
曾经老用findViewById,每次使用还得add cast一下今天看到一个视频(依据视频中使用的IDE判断,应该是几年前的视频了..),使用了一个方法,能够不用每次使用findViewById都去 ...
- log4j日志存储到数据库
一.前提条件 系统必须是使用LOG4J进行日志管理,否则方法无效. 系统必须包含commons-logging-xxx.jar,log4j-xxx.jar这两个JAR包,XXX为版本号. 二.操作步骤 ...
- 英语发音规则---M字母
英语发音规则---M字母 一.总结 一句话总结: 1.M发[m]音? monkey ['mʌŋkɪ] n. 猴子:顽童 come [kʌm] vi. 来 tomato [tə'mɑːtəʊ] n. 番 ...
- dnscat使用——整体感觉这个工具不完善,失败率很高,传文件时候没有完整性校验,我自己测试时通过域名转发失败,可能是其特征过于明显导致
git clone https://github.com/iagox86/nbtool make 然后就可以按照下面的官方说明进行操作了. 我的感受:整体感觉这个工具不完善,失败率很高,传文件时候没有 ...
- 转:utf8汉字编码16进制对照
http://blog.chinaunix.net/uid-25544300-id-3281847.html GB Unicode UTF-8 Chinese Character Co ...
- shp系列(一)——利用C++进行shp文件的读(打开)与写(创建)开言
博客背景和目的 最近在用C++写一个底层的东西,需要读取和创建shp文件.虽然接触shp文件已经几年了,但是对于shp文件内到底包含什么东西一直是一知半解.以前使用shp文件都是利用软件(如ArcGI ...
- 下压栈(LIFO)详解
写在前面的话: 一枚自学Java和算法的工科妹子. 算法学习书目:算法(第四版) Robert Sedgewick 算法视频教程:Coursera Algorithms Part1&2 本文 ...
- BZOJ 4028 分块
zrt当年是怎么想到的--. 思路: 考虑把序列分块 对于每块 存xor[i] 表示从本块开头到i的前缀异或和 把它扔进set里 存gcd[i]表示从本块开头到i的前缀gcd. 如果这一块的GCD和整 ...
- 在centOS 6.5下手动安装nginx1.9.x版本
第一步:首先安装Nginx的依赖环境 1.安装pcre-devel yum -y install pcre-devel #支持正则的pcre模块 比如安装 不然手动安装会报错 2.安 ...
- mybatis的二级缓存的使用
1.引入ehcache的jar包和mybatis整合ehcache的jar包: <!-- ehchache --> <dependency> <groupId>ne ...