zoukankan      html  css  js  c++  java
  • 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("
    升序后数据:");
    	for (int i = 0; i < 10; i++)
    	{
    		printf("%d ", data[i]);
    	}
    
    	//降序排序
    	sort(data, data+10, greater<int>());
    
    	printf("
    降序后数据:");
    	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("
    升序后数据:");
    	for (int i = 0; i < 10; i++)
    	{
    		printf("%d ", data[i]);
    	}
    
    	//降序排序
    	sort(data, data + 10, compare2);
    
    	printf("
    降序后数据:");
    	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("
    升序后数据:");
    	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    	{
    		printf("%d ", *it);
    	}
    	
    	//降序排序
    	sort(v.begin(), v.end(), greater<int>());
    
    	//遍历输出
    	printf("
    降序后数据:");
    	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("排序前数据:
    ");
    	for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
    	{
    		printf("%s; %d
    ", ((*it).name).c_str(), (*it).core);
    	}
    
    	//升序排序
    	sort(v.begin(), v.end(), compare1);
    
    	printf("
    升序后的数据:
    ");
    	for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
    	{
    		printf("%s; %d
    ", ((*it).name).c_str(), (*it).core);
    	}
    
    	//降序排序
    	sort(v.begin(), v.end(), compare2);
    	printf("
    降序后的数据:
    ");
    	for (vector<Student>::iterator it = v.begin(); it != v.end(); it++)
    	{
    		printf("%s; %d
    ", ((*it).name).c_str(), (*it).core);
    	}
    	getchar();
    	return 1;
    }

    欢迎加群交流:C/C++开发交流

  • 相关阅读:
    高级软件工程第八次作业LLS战队团队作业五
    Alpha阶段个人总结
    高级软件工程第七次作业:LLS战队Alpha敏捷冲刺7
    高级软件工程第七次作业:LLS战队Alpha敏捷冲刺6
    数独游戏界面功能
    数独棋盘
    调研《构建之法》指导下的全国高校的历届软工实践作品、全国互联网+竞赛、物联网竞赛、华为杯研究生作品赛、全国大学生服务外包赛等各类全国性大学生信息化相关的竞赛平台的历届作品
    高级软件工程课程的实践项目的自我目标
    Beta冲刺汇总博客
    团队作业9——第二次项目冲刺2(Beta阶段)
  • 原文地址:https://www.cnblogs.com/woniu201/p/11694596.html
Copyright © 2011-2022 走看看