C++:标准模板库Sort

xiaoxiao2021-02-28  14

一.概述

 

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++开发交流

 

 

转载请注明原文地址: https://www.6miu.com/read-850001.html

最新回复(0)