C++:标准模板库Sort
一.概述
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的更多相关文章
- 标准模板库(STL)学习指南之sort排序
对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算法也基本固定下来,不需要你再去花费心思 ...
- STL标准模板库(简介)
标准模板库(STL,Standard Template Library)是C++标准库的重要组成部分,包含了诸多在计算机科学领域里所常见的基本数据结构和基本算法,为广大C++程序员提供了一个可扩展的应 ...
- 【c++】标准模板库STL入门简介与常见用法
一.STL简介 1.什么是STL STL(Standard Template Library)标准模板库,主要由容器.迭代器.算法.函数对象.内存分配器和适配器六大部分组成.STL已是标准C++的一部 ...
- C++——string类和标准模板库
一.string类 1.构造函数 string实际上是basic_string<char>的一个typedef,同时省略了与内存管理相关的参数.size_type是一个依赖于实现的整型,是 ...
- STL 简介,标准模板库
这篇文章是关于C++语言的一个新的扩展--标准模板库的(Standard Template Library),也叫STL. 当我第一次打算写一篇关于STL的文章的时候,我不得不承认我当时低估了这个话 ...
- C++ 标准模板库(STL)
C++ 标准模板库(STL)C++ STL (Standard Template Library标准模板库) 是通用类模板和算法的集合,它提供给程序员一些标准的数据结构的实现如 queues(队列), ...
- STL(标准模板库)理论基础,容器,迭代器,算法
基本概念 STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.现然主要出现在C++中,但在被引入C++之前该技术就已经存在了很长的一段时间. ...
- STL(标准模板库)基本概念
一.什么是STL STL(Standard Template Library,标准模板库)的从广义上讲分为三类:algorithm(算法).container(容器)和iterator(迭代器),容器 ...
- (C/C++学习笔记) 二十二. 标准模板库
二十二. 标准模板库 ● STL基本介绍 标准模板库(STL, standard template library): C++提供的大量的函数模板(通用算法)和类模板. ※ 为什么我们一般不需要自己写 ...
随机推荐
- manjaro 滚动更新后无法开机,Failed to start load kernel modules,nvidia驱动导致
今天滚动后无法开机,启动时显示Faild to start load kernel modules,卡在后面无法进入登录界面 systemctl status systemd-modules-load ...
- 不用中间变量交换a和b的值?
a = b = a = a+b b = a-b a = a-b print(a,b) a = b = a = a^b b = b^a a = a^b print(a,b) a = b = a,b = ...
- [bzoj 3566][SHOI 2014]概率充电器
传送门 Description SHOI 概率充电器由 n-1 条导线连通了 n 个充电元件.进行充电时,每条导线是否可以导电以概率决定,每一个充电元件自身是否直接进行充电也由概率决定. 随后电能可以 ...
- JSP 9大内置对象以及作用
1.HttpServletRequest的 request对象作用:代表请求对象,用来接收客户端通过http协议连接传输到服务器端的数据. 2.HttpServletResponse的response ...
- cmake入门之内部构建
https://www.cnblogs.com/coderfenghc/tag/cmake/ https://cmake.org/cmake/help/v3.16/guide/tutorial/ind ...
- avalon里面倒计时功能2
// 转格式 timeStamp: function (second_time) { var time = parseInt(second_time) + "秒"; ) { ; ) ...
- 【2018.07.30】(广度优先搜索算法/队列)学习BFS算法小记
一些BFS参考的博客: https://blog.csdn.net/ldx19980108/article/details/78641127 https://blog.csdn.net/u011437 ...
- 高效使用Linux快捷键
命令行操作快捷键 Ctrl+a跳到本行的行首,Ctrl+e则跳到页尾. Ctrl+u删除当前光标前面的所有文字, ctrl+k-删除当前光标后面的所有文字 Ctrl+w删除光标前的单词 Backsap ...
- 火狐调试工具-DevTools
狐调试工具 - DevTools 咱们做写js 代码的时候,遇到的一个最大的问题就是调试问题,很多开发者在写 js 代码的时候,经常都非常痛苦.但是我们如果掌握好相应的调试工具,那么就可以比较游刃有余 ...
- Cross-Site Request Forgery (CSRF)
https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF) Overview Cross-Site Request Forger ...