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. Python模块路径查找

    本文主要介绍如何查找某个Python模块的绝对路径,下面以opencv模块的查找为例.有两种方法 第一种方法 打开一个终端,输入 python -v import cv2 最后一行显示如下 第二种方法 ...

  2. 简单易学的机器学习算法——神经网络之BP神经网络

    一.BP神经网络的概念     BP神经网络是一种多层的前馈神经网络,其基本的特点是:信号是前向传播的,而误差是反向传播的.详细来说.对于例如以下的仅仅含一个隐层的神经网络模型: watermark/ ...

  3. 经常使用的MySQL语句整理

    本文參考:http://www.blogjava.net/bolo 部分自己补充,长期更新 MySQL的SQL语句写法,除了那些主要的之外,另一些也算比較经常使用的,这里记录下来,以便以后查找. 好记 ...

  4. OpenLayers学习笔记3——使用jQuery UI美化界面设计

    PC端软件在开发是有较多的界面库能够选择,比方DevExpress.BCG.DotNetBar等,能够非常方便快捷的开发出一些炫酷的界面,近期在学习OpenLayers.涉及到web前端开发,在设计界 ...

  5. 具体图解 Flume介绍、安装配置

    写在前面一: 本文总结"Hadoop生态系统"中的当中一员--Apache Flume 写在前面二: 所用软件说明: 一.什么是Apache Flume 官网:Flume is a ...

  6. c12---数组

    // // main.c // 数组基本概念 // // Created by xiaomage on 15/6/9. // Copyright (c) 2015年 itcast. All right ...

  7. QT-项目文件说明

    前言:如题. 一.项目文件概述 文件 功能 helloworld.pro 包含了项目信息 helloworld.pro.user 用户信息 hellodialog.h 自定义类hellodialog的 ...

  8. Python的四个内置数据类型list, tuple, dict, set

    Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python的四个内置数据类型功不可没,他们即是list, tuple, dict, set.这里对他们进行一个简明的总结. List ...

  9. Excel—— [导入到数据库] or 将数据 [导入到Excel]

    将Excel导入到数据库实现如下: 前台代码: @model IEnumerable<Model.Student> @{ Layout = null; } <!DOCTYPE htm ...

  10. 洛谷P3746 [六省联考2017]组合数问题

    题目描述 组合数 C_n^mCnm​ 表示的是从 n 个互不相同的物品中选出 m 个物品的方案数.举个例子,从 (1;2;3) 三个物品中选择两个物品可以有 (1;2);(1;3);(2;3) 这三种 ...