STL(Standard Temlate Libray) 标准模板库 (一) sort用法

STL(Standard Temlate Libray) 标准模板库 (一)sort用法

内容:sort的多种用法
内容还算全面,如有不足日后学习中进行补充

STL概述

内容 : 包含一些常用的算法 例如排序查找 , 还有常用的数据类型 例如可变长数组 , 链表 , 字典等.

特点 : 使用方便 , 效率较高

头文件 : #include (英: algorithm 中译:算法 )

一 . 排序算法sort

用法一 (整形数组从小到大排序)

对基本类型的数组从小到大排序:

       sort ( 数组名 + n1 , 数组名 + n2) ;

       n1 和 n2 都是 int 类型得表达式

       如果n1 = 0,+ n1可以不写

将数组中下标范围为[n1 , n2) 的元素从小到大排序.

范例:

Cout函数为自写程序用于输出数组结果
	int a[] = {
    
     15,4,3,9,7,2,6 };
	sort(a, a + 7);       // 对整个数组从小到大排列
	Cout(a);              // 结果 : 2 3 4 6 7 9 15
	int b[] = {
    
     15,4,3,9,7,2,6 };
	sort(b, b + 3);       // 排序从b0到b2
	Cout(b);              // 结果 : 3 4 15 9 7 2 6 
	int c[] = {
    
     15,4,3,9,7,2,6 };
	sort(c + 2, c + 5);   // 排序从c2到c4
	Cout(c);              // 结果 : 15 4 3 7 9 2 6

用法二 (对元素类型为 T 的基本类型数组 从大到小/从小到大 排序)

用法: sort( 数组名 + n1 , 数组名 + n2 , greater<T>() );
用法: sort( 数组名 + n1 , 数组名 + n2 ,    less<T>() );

greater 和 less 分别是c++中内置 从大到小 , 从小到大的排序模板; *头文件*

// function 头文件中定义了大量模板,用来声明函数对象
// greater 和 less是其中的两个常用模板
template <class T> struct greater {
    
    
  bool operator() (const T& x, const T& y) const {
    
    return x>y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};
template <class T> struct less {
    
    
  bool operator() (const T& x, const T& y) const {
    
    return x<y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};

范例:

	int a[] = {
    
     15,4,3,9,7,2,6 };
	sort(a + 1, a + 4, greater<int>());    // 从大到小排序 int类型数组a 从a1到a3
	Cout(a);                               // 结果: 15 9 4 3 7 2 6
	int b[] = {
    
     15,4,3,9,7,2,6 };
	sort(b + 1, b + 4, less<int>());       // 从小到大排序 int类型数组a 从b1到b3
	Cout(b);                               // 结果: 15 3 4 9 7 2 6

用法三 (自定义排序规则)

  1. 用自定义的排序规则, 对任何类型T的数组排序;
用法: sort(数组名 + n1 , 数组名 + n2 , 排序规则结构名() );
  1. 排序规则结构的定义方式:
struct 结构名
{
    
    
        bool operator()( const T & a1,const T & a2) const {
    
    
                // 若a1应该在a2前面, 则返回true
                // 否则返回false
        }
}
范例:
  1. 创建模板:
struct Rule1 // 按从大到小排序
{
    
    
	bool operator()(const int& a1, const int& a2) const {
    
    
		return a1 > a2; //正确返回true 错误返回false,为true是a1应该在前面,那么就保证了大的数排在前面
	}
};
struct Rule2 // 按个位数大小排序
{
    
    
	bool operator()(const int& a1, const int& a2)const {
    
    
		return a1 % 10 < a2 % 10;
	}
};
  1. 调用模板:
int main() {
    
    
	int a[] = {
    
     12,45,3,98,21,7 };
	sort(a, a + sizeof(a) / sizeof(int));            // 从小到大排序整个数组
	Cout(a);                                         // 结果: 3 7 12 21 45 98
	sort(a, a + sizeof(a) / sizeof(int), Rule1());   // 调用Rule1模板 从大到小排序
	Cout(a);                                         // 结果: 98 45 21 12 7 3
	sort(a, a + sizeof(a) / sizeof(int), Rule2());   // 调用Rule2模板 按个位数从小到大排序
	Cout(a);                                         // 结果: 21 12 3 45 7 98
	return 0;
}

用法四 (用sort对结构数组进行排序)

  1. 结构体初始化:
struct Student {
    
    
	char name[20];
	int id;
	double gpa;
};
Student Students[] = {
    
    
	{
    
    "Jack",112,3.4},{
    
    "Mary",102,3.8},{
    
    "Kate",117,3.9},
	{
    
    "Ala",333,3.5},{
    
    "Zero",101,4.0} };
  1. 定义结构体排序模板
#pragma warning(disable:4996) // vs2019认为stricmp不安全,使用stricmp前加上此代码可忽略
struct StudentRule1 {
    
     //按姓名从小到大排序
	bool operator()(const Student& s1, const Student& s2)const {
    
    
		if (stricmp(s1.name, s2.name) < 0)
			return true;
		return false;
	}
};

struct StudentRule2 {
    
     //按id从小到大排序
	bool operator()(const Student& s1, const Student& s2)const {
    
    
		return s1.id < s2.id;
	}
};

struct StudentRule3 {
    
     //按gpa从高到低排序
	bool operator()(const Student& s1, const Student& s2)const {
    
    
		return s1.gpa > s2.gpa;
	}
};
  1. 输出函数定义与声明
void PrintStudents(Student s[], int size);
void PrintStudents(Student s[], int size) {
    
    
	for (int i = 0; i < size; i++)
		cout << " (" << s[i].name << "," << s[i].id << "," << s[i].gpa << ") ";
	cout << endl;
}
  1. 输出sort结果
int main() {
    
    
	int n = sizeof(Students) / sizeof(Student);
	sort(Students, Students + n, StudentRule1()); //按名字从小到大排序
	PrintStudents(Students, n);
    //结果:(A1a,333,3.5) (Jack,112,3.4) (Mary,102,3.8) (Mary,117,3.9) (Zero,101,4)
	sort(Students, Students + n, StudentRule2()); //按id从小到大排序
	PrintStudents(Students, n);
    //结果:(Zero,101,4) (Mary,102,3.8) (Jack,112,3.4) (Mary,117,3.9) (Ala,333,3.5)
	sort(Students, Students + n, StudentRule3()); //按gpa从高到低排序
	PrintStudents(Students, n);
    //结果:(Zero,101,4) (Kate,117,3.9) (Mary,102,3.8) (Ala,333,3.5) (Jack,112,3.4)
	return 0;
}

随后下期内容:STL系列(二)(三)(四)(五)
指针系列…
点个关注再走呗!

猜你喜欢

转载自blog.csdn.net/zhuiyizhifengfu/article/details/113533631