网站首页 > 教程分享 正文
一.概述
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) {}
stringname;
intcore;
};
//升序比较函数
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;
}
猜你喜欢
- 2024-10-11 Java中Arrays的两种排序方法(sort和parallelSort)比较
- 2024-10-11 Excel中的排序函数RANK,这6种使用技巧你一定不能错过
- 2024-10-11 七种排序算法 冒泡,选择,插入,希尔,快速,归并,堆
- 2024-10-11 深入浅出Redis:Redis的排序命令Sort
- 2024-10-11 「收藏」JS数组排序技巧汇总(冒泡、sort、快速、希尔等排序)
- 2024-10-11 R语言sort和order排序函数(r语言给数据排序)
- 2024-10-11 vba如何通过调用sort方法实现排序?跟着文章解开心中的谜题!
- 2024-10-11 用SORTBY函数进行多条件排序 #excel技巧
- 2024-10-11 python数据排序-sorted与sort(python中的sorted排序)
- 2024-10-11 Linux基础知识之sort排序查看硬盘SN
你 发表评论:
欢迎- 最近发表
-
- 免费10年VPS-serv00服务器,注册与自动化保号
- Consul微服务注册中心使用指南
- 谷歌云代理商:注册谷歌云服务器需要准备哪些资料?
- steam账号注册不了/注册失败?好用的解决方法看这里
- 微服务架构中的服务注册与发现有哪些?Zookeeper、Eu
- # 从浅入深 学习 SpringCloud 微服务架构(三)注册中心 Eureka(1)
- 一文深入理解AP架构Nacos注册原理
- 群晖NAS本地搭建NVIDIA v-GPU License Server 授权许可服务器的教程
- IDEA 2024解决We could not validate your license XX
- 保障数据完整性:深入解析Oracle数据库的主键和外键约束
- 标签列表
-
- css导航条 (66)
- sqlinsert (63)
- js提交表单 (60)
- param (62)
- parentelement (65)
- jquery分享 (62)
- check约束 (64)
- curl_init (68)
- sql if语句 (69)
- import (66)
- chmod文件夹 (71)
- clearinterval (71)
- pythonrange (62)
- 数组长度 (61)
- javafx (59)
- 全局消息钩子 (64)
- sort排序 (62)
- jdbc (69)
- php网页源码 (59)
- assert h (69)
- httpclientjar (60)
- postgresql conf (59)
- winform开发 (59)
- mysql数字类型 (71)
- drawimage (61)
本文暂时没有评论,来添加一个吧(●'◡'●)