一.概述

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. Lua 常用函数 一

    lua_getallocf lua_Alloc lua_getallocf (lua_State *L, void **ud); 返回给定状态机的内存分配器函数.如果 ud 不是 NULL ,Lua ...

  2. C#中指针的简单使用

    原来C#不仅仅支持和C/C++中指针(或者说是引用)很像的委托delegate,还支持在unsafe代码块中使用指针,从而写非托管的代码(人为不让垃圾回收机制来管理相应的内存).在unsafe中就可以 ...

  3. Linux中查看某个端口占用情况

    譬如在linux中排查某个端口是否被占用,可以通过如下命令进行排查,排查方法如下: 1: 排查 : 应用是否被人debug.  8787 为端口号 netstat -anp |grep 8787 图中 ...

  4. 简单动态字符串-redis设计与实现

    简单动态字符串 Sds (Simple Dynamic String,简单动态字符串)是 Redis 底层所使用的字符串表示, 几乎所有的 Redis 模块中都用了 sds. 本章将对 sds 的实现 ...

  5. 监控zabbix 3.4.11异常通过邮件报警步骤

    监控的目的一个是可以查看历史状态,可以对比零晨和工作区间数据的对比,以便后期进行优化指导.还有一个是报警,总不能等到服务器出现异常了才去从头查是什么问题吧.所以这篇主要介绍报警中最基础的一个 配置邮件 ...

  6. 设计自用的golang日志模块

    设计自用的golang日志模块 golang的原生日志模块不能满足需求,而开源的第三方包,也不完全够用.用户较多的logrus,却没有rotate功能,这已经是众所周知的.对于运维来说,当然是希望日志 ...

  7. c++ 套接字 --->2002 java NIO --->netty

    c++ 套接字 --->2002 java NIO --->netty

  8. xss绕过姿势

    #未完待续... 00x1.绕过 magic_quotes_gpc magic_quotes_gpc=ON 是php中的安全设置,开启后会把一些特殊字符进行轮换, 比如: ' 会被转换为 \' 再比如 ...

  9. springBoot注解搜集

    一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...

  10. iOS 将一个UIImage缩放到指定Size

    这方法挺实用的,直接调用就可以得到想要的size. //将一个UIImage缩放变换到指定Size -(UIImage*) OriginImage:(UIImage *)image scaleToSi ...