C++STL排序查找算法

STL: (Standard Template Library) 标准模板库
使用STL的排序和查找算法需要调用头文件

#include <algorithm>

排序

示例一:

sort可以对基本类型的数组从小到大排序: sort(数组名+n1,数组名+n2),将数组中下标范围为[n1,n2)的元素从小到大排序,注意下标为n2的元素不在排序区间内。

int a[10]={
    
    3,0,9,7,1,5,4,2,8,6};
sort(a,a+10);	

输出

0 1 2 3 4 5 6 7 8 9

示例二:

也可以从大到小排序

int a[10]={
    
    3,0,9,7,1,5,4,2,8,6};
sort(a,a+10,greater<int>());

输出

9 8 7 6 5 4 3 2 1 0

示例三:

sort也可以自定义排序规则

struct Rule1 //按从大到小排序
{
    
    
	bool operator()( const int & a1,const int & a2) const {
    
    
		return a1 > a2;
	}
};

struct Rule2 //按个位数从小到大排序
{
    
    
	bool operator()( const int & a1,const int & a2) const {
    
    
		return a1%10 < a2%10;
	}
};


int main()
{
    
    
	int a[] = {
    
    9,13,56,90,15,77,82,93,45};
	sort(a,a+sizeof(a)/sizeof(int)); //从小到大
	sort(a,a+sizeof(a)/sizeof(int),Rule1()); //从大到小
	sort(a,a+sizeof(a)/sizeof(int),Rule2()); //按个位数从小到大
	return 0;
}

输出

  1. 9 13 15 45 56 77 82 90 93
  2. 93 90 82 77 56 45 15 13 9
  3. 90 82 93 13 45 15 56 77 9
struct Student {
    
    
	char name[20];
	int id;
	double gpa;
};

Student students [] = {
    
     
	{
    
    "Jack",112,3.4},{
    
    "Mary",102,3.8},{
    
    "Mary",117,3.9},
	{
    
    "Ala",333,3.5},{
    
    "Zero",101,4.0}
};

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;
	}
};

int main()
{
    
    
	int n = sizeof(students) / sizeof(Student);
	sort(students,students+n,StudentRule1()); //按姓名从小到大排
	sort(students,students+n,StudentRule2()); //按id从小到大排
	sort(students,students+n,StudentRule3()); //按gpa从高到低排
	return 0;
}

输出

(Ala,333,3.5) (Jack,112,3.4) (Mary,102,3.8) (Mary,117,3.9) (Zero,101,4)
(Zero,101,4) (Mary,102,3.8) (Jack,112,3.4) (Mary,117,3.9) (Ala,333,3.5)
(Zero,101,4) (Mary,117,3.9) (Mary,102,3.8) (Ala,333,3.5) (Jack,112,3.4)

另一种写法
例:将用时按时分秒升序排序

struct node
{
    
    
	int h,m,s;
}a[5005];

bool cmp(const node&lhs, const node&rhs)
{
    
    
	return lhs.h<rhs.h||(lhs.h==rhs.h&&lhs.m<rhs.m)||
		(lhs.h==rhs.h&&lhs.m==rhs.m&&lhs.s<rhs.s);
}

sort(a,a+n,cmp);

输入:

11 20 20
11 15 12
14 20 14

输出:

11 15 12
11 20 20
14 20 14

示例四:

sort可以对string类排序

string s="zginlabdse";
sort(s.begin(),s.end());

输出

abdegilnsz

查找

STL提供在排好序的数组上进行二分查找的算法
binary_search
lower_bound
upper_bound

binary_search(数组名+n1,数组名+n2,值);
查找区间为下标范围为[n1,n2)的元素,下标为n2的元素不在查找区间内
在该区间内查找"等于"值”的元素,返回值为true(找到)或false(没找到)
"等于"的含义: a 等于 B <=> a < b和b < a都不成立

int a[]={
    
    1,3,5,8,11,13,16,19,21};
bool i=binary_search(a,a+9,13);

输出

1

lower_bound和upper_bound返回元素位置的迭代器(或指针)

lower_bound查找第一个大于x的元素

upper_bound查找第一个大于等于x的元素

int a[]={
    
    1,3,5,8,11,13,16,19,21};
int i=lower_bound(a,a+9,8)-a;
int j=upper_bound(a,a+9,8)-a;	

输出

i=3
j=4

猜你喜欢

转载自blog.csdn.net/weixin_46155777/article/details/108897625
今日推荐