stl的sort函数与二分查找

sort函数小结

1.要使用头文件#include<algorithm>和using namespace std;

2.对基本类型数组排序

.sort(数组名+n1,数组名+n2)         对下标范围[n1,n2)的元素从小到大排序,下标为n2的元素不在排序区间内 

 sort(数组名+n1,数组名+n2,greater<T>)   T为数组的类型int double 从大到小排序

3。用自定义的排序规则,对任何类型T的数组排序

  sort(数组名+n1,数组名+n2,排序规则结构名())
排序规则结构的定义方式

struct 结构名
{
    bool operator() (const T & a1,const T & a2) const {
        //若a1应该在a2前面,则返回true,否则返回false 
    }   
} ;

对结构体排序

struct Student {
    char name[20];
    int id;
    double gpa;
};

struct StdentRule1{ //按姓名从小到大排 
    bool operator () (const Student & s1,const Student & s2) const {
        if(stricmp(s1.name,s2.name)<0)
            return true;
        return false;
    }
};

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

sizeof(a)/sizeof(int) 可以求int类型数组的长度 

 

 

 

stl的二分查找

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

binary_search;

 

1. 在从小到大排好序的基本类型数组上进行二分查找

 binary_search(数组名+n1,数组名+n2,值);

查找区间为下标范围为[n1,n2)的元素,下标为n2的元素不在查找区间,内在该区间内查找在该区间内等于值(“等于”的含义:a等于b<=>a必须在b前面 和 b必须在a前面都不成立;不是==)的元素,返回值为true(找到)或false(没找到)

 

2. 在用自定义排序规则排好序、元素为任意的T类型的数组中进行二分查找

binary_search(数组名+n1,数组名+n2,值,排序规则结构名());

查找区间为下标范围为[n1,n2)的元素,下标为n2的元素不在查找区间,内在该区间内查找在该区间内等于值(“等于”的含义:a等于b<=>a必须在b前面 和 b必须在a前面都不成立;不是==)的元素,返回值为true(找到)或false(没找到)

查找的排序规则必须和排序时的规则一样 否则无意义

#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
struct Rule
{
    bool operator() ( const int & a1,const int & a2) {
            return a1%10 < a2%10;
    }
};

void Print(int a[],int size) {
    for(int i = 0;i < size;++i) {
            cout << a[i] << ",";
    }
    cout << endl;
}

int main()
{
    int a[] =  { 12,45,3,98,21,7};
    sort(a,a+6);
    Print(a,6); 
    cout << "result:" << binary_search(a,a+6,12) << endl;
    cout << "result:" << binary_search(a,a+6,77) << endl;
    sort(a,a+6,Rule());
    Print(a,6);
    cout << "result:" << binary_search(a,a+6,7) << endl;
    cout << "result:" << binary_search(a,a+6,8,Rule()) << endl;
    return 0;
}

 OUTPUT

3,7,12,21,45,98,
result:1
result:0
21,12,3,45,7,98,
result:0 // 查找的排序规则和排序时的规则不一样 无意义
result:1 // 由于等于的含义不是==,98和8的排序规则满足a必须在b前面 和 b必须在a前面都不成立,所以输出1

 

 

 

lower_bound(查找下界)

1.在对元素类型为T的从小到大排好序的基本类型的数组中进行查找

T * lower_bound(数组名+n1,数组名+n2,值);

返回一个 指针T * p;

*p是查找区间里下标最小的,大于等于值的元素。若找不到,指向下标为n2的元素

 

2.在元素为任意类型,按自定义排序规则排好序的数组中进行查找

T * lower_bound(数组名+n1,数组名+n2,值,排序规则结构名());

返回一个 指针T * p;

*p是查找区间里下标最小的,按自定义排序规则,可以排在值后面的元素(大于等于)。若找不到,指向下标为n2的元素

 

upper_bound(查找上界)

1.在对元素类型为T的从小到大排好序的基本类型的数组中进行查找

T * upper_bound(数组名+n1,数组名+n2,值);

返回一个 指针T * p;

*p是查找区间里下标最小的,大于值的元素。若找不到,指向下标为n2的元素

 

2.在元素为任意的T类型,按自定义排序规则排好序的数组中进行查找

T * upper_bound(数组名+n1,数组名+n2,值,排序规则结构名());

返回一个 指针T * p;

*p是查找区间里下标最小的,按自定义排序规则,必须排在值后面的元素。若找不到,指向下标为n2的元素

#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
struct Rule
{
    bool operator() ( const int & a1,const int & a2) {
            return a1%10 < a2%10;
    }
};

void Print(int a[],int size) {
    for(int i = 0;i < size;++i) {
            cout << a[i] << ",";
    }
    cout << endl;
}

int main()
{
    int a[] =  { 12,5,3,5,98,21,7};
    sort(a,a+7);
    Print(a,7); 
    int *p = lower_bound(a,a+7,5);
    cout << *p << "," << p-a << endl;
    p = upper_bound(a,a+7,5);
    cout << *p <<  endl;
    cout << *upper_bound(a,a+7,13) <<endl;
    sort(a,a+7,Rule());
    Print(a,7);
    cout << * lower_bound(a,a+7,16,Rule()) << endl;
    cout << lower_bound(a,a+7,25,Rule()) - a << endl;
    cout << upper_bound(a,a+7,18,Rule()) - a << endl;
    if( upper_bound(a,a+7,18,Rule())== a+7){
        cout << "not found" << endl;
    }
    cout << *upper_bound(a,a+7,5,Rule()) << endl;
    cout << *upper_bound(a,a+7,4,Rule()) << endl;
    return 0;
}

 OUTPUT

3,5,5,7,12,21,98,
5,1
7
21
21,12,3,5,5,7,98,
7
3
7
not found  
7
5

 

posted @ 2021-01-20 20:19  TXCCXT  阅读(95)  评论(0)    收藏  举报