一.概述

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. 在 Ubuntu 18.04 /centos7上安装 Python 3.7

    扩展源安装 sudo apt update sudo apt install software-properties-common sudo add-apt-repository ppa:deadsn ...

  2. CF1174B Ehab Is an Odd Person(排序+结论)

    做法 一个显然的结论就是如果至少有一个奇数和一个偶数,那么是可以随意调整的,也就是升序排序 否则不可以进行任何操作 Code #include<bits/stdc++.h> using n ...

  3. Spring Cloud Gateway(八):其它路由谓词工厂

    本文基于 spring cloud gateway 2.0.1 6.基于Cookie的谓词工厂 CookieRoutePredicateFactory 是 Cookie 类型的路由断言工厂,接收两个参 ...

  4. docker搭建hadoop HA出错问题总结记录。

    错误1: ssh连接云主机: ssh root@39.106.xx.xx 报错:THE AUTHENTICITY OF HOST XX CAN’T BE ESTABLISHED 解决办法: ssh - ...

  5. Windows下OpenFOAM开发及使用环境配置指南 (1)【转载】

    转载自:http://openfoam.blog.sohu.com/158614863.html *************************************************** ...

  6. 【代码】python 绘图(以faster rcnn 的 loss 曲线为例)

    # -*- coding=utf-8 -*-''' import matplotlib.pyplot as plt import re logs=open('loss').read() #print ...

  7. 简易版最长序列(map映射)

      题目描述 给你一组数(未排序),请你写设计一个程序:求出里面个数最多的数.并输出这个数的长度. 例如:给你的数是:1. 2. 3. 3. 4. 4. 5. 5. 5 .6, 其中只有6组数:1, ...

  8. php手记之03-tp5渲染视图

    // 渲染视图 模块@文件/文件名 // return view('index@index/test'); // return view('index@test/test'); // return $ ...

  9. Qt之模型/视图(自定义风格)

    Qt之模型/视图(自定义风格) 关于自定义风格是针对视图与委托而言的,使用事件与QSS都可以进行处理,今天关于美化的细节讲解一下. 先看下图: 先撇开界面的美观性(萝卜青菜,各有所爱),就现有的这些风 ...

  10. Hive中导入Oracle数据错误:Listener refused the connection with the following error: ORA-12505

    问题: 今天往Hive中导入Oracle数据的时候碰到了如下错误:Listener refused the connection with the following error: ORA-12505 ...