一.概述

STL几乎封装了所用的数据结构中的算法,这里主要介绍排序算法的使用,指定排序迭代器区间后,即可实现排序功能。

所需头文件#include <algorithm>

sort函数:对给定区间所有元素进行排序,默认两个参数或三个参数,第一个参数待排序区间的首地址,第二个参数待排序区间尾地址的下一个地址

 只传递两个参数默认使用升序排序,如想按照降序排序需要传入第三个参数,第三个参数可以使用库函数也可以自定义比较函数。

 第三个参数使用库函数:

 包含头文件<functional>

 升序:less<data-type>()

 降序:greater<data-type>()

二.使用

2.1 对数组排序

2.1.1 使用库函数作为比较函数

#include "stdio.h"
#include "stdlib.h"
#include <algorithm>
#include <functional> using namespace std; int main1()
{
int data[10] = {0};
for (int i=0; i<10; i++)
{
data[i] = rand() % 10;
}
printf("排序前数据:");
for (int i=0; i<10; i++)
{
printf("%d ", data[i]);
} //升序排序
sort(data, data+10, less<int>()); printf("\n升序后数据:");
for (int i = 0; i < 10; i++)
{
printf("%d ", data[i]);
} //降序排序
sort(data, data+10, greater<int>()); printf("\n降序后数据:");
for (int i = 0; i < 10; i++)
{
printf("%d ", data[i]);
}
getchar();
return 1;
}

2.1.2 自定义比较函数

#include "stdio.h"
#include "stdlib.h"
#include <algorithm> using namespace std; //升序比较函数
bool compare1(const int& a , const int& b)
{
return a < b;
} //降序比较函数
bool compare2(const int& a, const int& b)
{
return a > b;
} int main()
{
int data[10] = { 0 };
for (int i = 0; i < 10; i++)
{
data[i] = rand() % 10;
}
printf("排序前数据:");
for (int i = 0; i < 10; i++)
{
printf("%d ", data[i]);
} //升序排序
sort(data, data + 10, compare1); printf("\n升序后数据:");
for (int i = 0; i < 10; i++)
{
printf("%d ", data[i]);
} //降序排序
sort(data, data + 10, compare2); printf("\n降序后数据:");
for (int i = 0; i < 10; i++)
{
printf("%d ", data[i]);
}
getchar();
return 1;
}

2.2 对vector排序

2.2.1 对vector存放的整形排序

#include "stdio.h"
#include "stdlib.h"
#include <vector>
#include <algorithm>
#include <functional> using namespace std; //升序比较函数
int compare1(const int &a, const int &b)
{
return a < b;
} //降序比较函数
int compare2(const int &a, const int &b)
{
return a > b;
} int main()
{
vector<int> v;
for (int i=0; i<10; i++)
{
v.push_back(rand() % 10);
} //遍历输出
printf("排序前数据:");
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%d ", *it);
} //升序排序
sort(v.begin(), v.end(), compare1); //遍历输出
printf("\n升序后数据:");
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%d ", *it);
} //降序排序
sort(v.begin(), v.end(), greater<int>()); //遍历输出
printf("\n降序后数据:");
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%d ", *it);
} getchar();
return 1;
}

2.2.2 对vector存放的类成员变量排序

#include "stdio.h"
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std; class Student {
public:
Student(string n, int c) :name(n), core(c) {} string name;
int core;
}; //升序比较函数
bool compare1(const Student& s1, const Student& s2)
{
return s1.core < s2.core;
} //降序比较函数
bool compare2(const Student& s1, const Student& s2)
{
return s1.core > s2.core;
} int main()
{
vector<Student> v;
Student s1("aaaa", 97);
Student s2("bbbb", 99);
Student s3("cccc", 95); v.push_back(s1);
v.push_back(s2);
v.push_back(s3); printf("排序前数据:\n");
for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%s; %d\n", ((*it).name).c_str(), (*it).core);
} //升序排序
sort(v.begin(), v.end(), compare1); printf("\n升序后的数据:\n");
for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%s; %d\n", ((*it).name).c_str(), (*it).core);
} //降序排序
sort(v.begin(), v.end(), compare2);
printf("\n降序后的数据:\n");
for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
{
printf("%s; %d\n", ((*it).name).c_str(), (*it).core);
}
getchar();
return 1;
}

欢迎加群交流:C/C++开发交流

C++:标准模板库Sort的更多相关文章

  1. 标准模板库(STL)学习指南之sort排序

    对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算法也基本固定下来,不需要你再去花费心思 ...

  2. STL标准模板库(简介)

    标准模板库(STL,Standard Template Library)是C++标准库的重要组成部分,包含了诸多在计算机科学领域里所常见的基本数据结构和基本算法,为广大C++程序员提供了一个可扩展的应 ...

  3. 【c++】标准模板库STL入门简介与常见用法

    一.STL简介 1.什么是STL STL(Standard Template Library)标准模板库,主要由容器.迭代器.算法.函数对象.内存分配器和适配器六大部分组成.STL已是标准C++的一部 ...

  4. C++——string类和标准模板库

    一.string类 1.构造函数 string实际上是basic_string<char>的一个typedef,同时省略了与内存管理相关的参数.size_type是一个依赖于实现的整型,是 ...

  5. STL 简介,标准模板库

    这篇文章是关于C++语言的一个新的扩展--标准模板库的(Standard Template Library),也叫STL.  当我第一次打算写一篇关于STL的文章的时候,我不得不承认我当时低估了这个话 ...

  6. C++ 标准模板库(STL)

    C++ 标准模板库(STL)C++ STL (Standard Template Library标准模板库) 是通用类模板和算法的集合,它提供给程序员一些标准的数据结构的实现如 queues(队列), ...

  7. STL(标准模板库)理论基础,容器,迭代器,算法

    基本概念 STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.现然主要出现在C++中,但在被引入C++之前该技术就已经存在了很长的一段时间.   ...

  8. STL(标准模板库)基本概念

    一.什么是STL STL(Standard Template Library,标准模板库)的从广义上讲分为三类:algorithm(算法).container(容器)和iterator(迭代器),容器 ...

  9. (C/C++学习笔记) 二十二. 标准模板库

    二十二. 标准模板库 ● STL基本介绍 标准模板库(STL, standard template library): C++提供的大量的函数模板(通用算法)和类模板. ※ 为什么我们一般不需要自己写 ...

随机推荐

  1. [vsftpd] ubuntu14.04 ansible剧本安装vsftpd流程及报错排查

    需求: 在ubuntu14.04机器上搭建ftp服务,ftp账号通过winscp软件登录后,仅可增删改/data/wwwroot目录. 一.安装步骤 1.apt 安装vsftpd apt-get in ...

  2. Java学习日记基础篇(八) —— 二进制、位运算、位移运算

    二进制 二进制是逢2进位的进位置,0,1是基本算符 原码反码补码 在基本数据类型那里,有详细解释 二进制的最高位数是符号位:0表示整数,1表示负数 正数的原码,反码,补码都一样 负数的反码 = 它的原 ...

  3. 持续api管理翻译

    - 书籍内容 > 地址: https://www.safaribooksonline.com/library/view/continuous-api-management/97814920435 ...

  4. python3编程基础之一:代码封装

    几乎现代的编程语言都支持函数,函数是代码段的封装,并能实现一特定功能,并能重复使用的代码单位.之前的pow()和sqrt()和print()和input()等类似的内置函数,就是python内部已经实 ...

  5. 搞定TADA68机械键盘的固件修改

    一.先到http://config.qmk.fm,在键盘型号中选择tada68,千万不要选择错了,可在kdemap name中为自己的键位名个名,例如:vimgood 二.修改自己的键位,都是可视化的 ...

  6. harukaの赛前日常

    REMEMBER US. haruka是可爱的孩子. 如题,此博客用来记录我停课后的日常. Dear Diary 10.8 上午考试. T1,直接枚举每一个点最后一次被修改的情况.(100pts) T ...

  7. Markdown 小记

    在学习Markdown之前,对稍有轻微强迫症的我来说,写博客和做笔记是一件很痛苦的事.废话不多说直接来看,偷偷吐槽:不知道咋在博客园配置Markdown,以后如果学会了回来补充. 标题 #hello一 ...

  8. Selenium 常用JS

    滑动scroll: window.scrollTo(0,document.body.scrollHeight);

  9. spring boot + vue 前后分离实现登录功能(二)

    安装 axios 进行路由转发 npm install axios --save-dev 或者 cnpm install axios --save-dev 修改 Main.js 新增 var axio ...

  10. SpringBoot表单数据校验

    Springboot中使用了Hibernate-validate作为默认表单数据校验框架 在实体类上的具体字段添加注解 public class User { @NotBlank private St ...