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)的更多相关文章

  1. 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) ...

  2. 《Algorithms算法》笔记:元素排序(4)——凸包问题

    <Algorithms算法>笔记:元素排序(4)——凸包问题 Algorithms算法笔记元素排序4凸包问题 凸包问题 凸包问题的应用 凸包的几何性质 Graham 扫描算法 代码 凸包问 ...

  3. 《Algorithms算法》笔记:元素排序(3)——洗牌算法

    <Algorithms算法>笔记:元素排序(3)——洗牌算法 Algorithms算法笔记元素排序3洗牌算法 洗牌算法 排序洗牌 Knuth洗牌 Knuth洗牌代码 洗牌算法 洗牌的思想很 ...

  4. 《Algorithm算法》笔记:元素排序(2)——希尔排序

    <Algorithm算法>笔记:元素排序(2)——希尔排序 Algorithm算法笔记元素排序2希尔排序 希尔排序思想 为什么是插入排序 h的确定方法 希尔排序的特点 代码 有关排序的介绍 ...

  5. 《Algorithms算法》笔记:元素排序(1)——简单排序

    <Algorithms算法>元素排序(1)——简单排序 Algorithms算法元素排序1简单排序 排序问题 1 回调函数 2Java中回调函数的路线图 3 全序 4 Comparable ...

  6. STL_算法_依据第n个元素排序(nth_element)

    C++ Primer 学习中... 简单记录下我的学习过程 (代码为主) //全部容器适用 nth_element(b,n,e) nth_element(b,n,e,p) 对照:partition() ...

  7. STL_算法_局部排序(partial_sort、partial_sort_copy)

    C++ Primer 学习中. . . 简单记录下我的学习过程 (代码为主) /***************************************** // partial_sort(b, ...

  8. STL_算法_元素计数(count、count_if)

    C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) count . count_if #include<iostream> #include<cstdio&g ...

  9. STL_算法_查找算法(lower_bound、upper_bound、equal_range)

    C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) //全部容器适用(O(log(n)))    已序区间查找算法 lower_bound()        //找第一个符合的 ...

随机推荐

  1. UIButton上字体的对齐方式

    设置UIButton上字体的对齐方式,不是用: [Button.titleLabel setTextAlignment:UITextAlignmentCenter]; 而是用: [Button set ...

  2. 多校第二场 1004 hdu 5303 Delicious Apples(背包+贪心)

    题目链接: 点击打开链接 题目大意: 在一个周长为L的环上.给出n棵苹果树.苹果树的位置是xi,苹果树是ai,苹果商店在0位置,人的篮子最大容量为k,问最少做多远的距离可以把苹果都运到店里 题目分析: ...

  3. 回车登录(支持IE 和 火狐等浏览器)

    $("body").keydown(function(e){ var curKey = e.which; if(curKey == 13){ $("#Btn_login& ...

  4. Linux 程序设计学习笔记----Linux下文件类型和属性管理

    转载请注明出处:http://blog.csdn.net/suool/article/details/38318225 部分内容整理自网络,在此感谢各位大神. Linux文件类型和权限 数据表示 文件 ...

  5. Android自己定义控件系列三:自己定义开关button(二)

    接上一篇自己定义开关button(一)的内容继续.上一次实现了一个开关button的基本功能.即自己定义了一个控件.开关button,实现了点击切换开关状态的功能.今天我们想在此基础之上.进一步实现触 ...

  6. 2015.04.30,外语,读书笔记-《Word Power Made Easy》 14 “如何谈论日常现象” SESSION 40

    1. money, and what it will buy penury(['penjuri] n. 贫穷,拮据),来自拉丁词语penuria(need,needness的意思),主要指缺乏财富资源 ...

  7. 5.listview(QStringList QStringListModel)

    UI mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include < ...

  8. GradientDrawable类的利用动态设置样式中的颜色

    1.xml样式文件 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android=& ...

  9. Android截图<包括Alertdiaog>

    1.使用的系统内部的截屏方法…… 2. public class MainActivity extends AppCompatActivity { private static final Strin ...

  10. pixhawk入门知识

    Pixhawk是一种先进的自动驾驶仪,由PX4开放硬件项目设计和3D机器人制造.它具有来自ST公司先进的处理器和传感器技术,以及NuttX实时操作系统,能够实现惊人的性能,灵活性和可靠性控制任何自主飞 ...