STL-lower_bound

lower_bound

 编辑 讨论

本词条缺少概述图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧!

lower_bound()返回一个 iterator 它指向在[first,last)标记的有序序列中可以插入value,而不会破坏容器顺序的第一个位置,而这个位置标记了一个不小于value 的值。该函数为C++ STL内的函数。

中文名

lower_bound

函数原型

template<classForward

举    例

vector<int>nums;

注意事项

必须确定序列为有序序列

目录

  1. C++ STL
  2. ▪ 函数原型
  3. ▪ 函数介绍
  1. ▪ 注意事项
  2. 举例
  3. ▪ 第一个版本
  1. ▪ 第二个版本

C++ STL

编辑

函数原型

第一个版本(default):

1

template <class ForwardIterator, class T>  ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,                               const T& val);

[1]  第二个版本(custom):

1

template <class ForwardIterator, class T, class Compare>  ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,                               const T& val, Compare comp);

函数介绍

例如,有如下序列:

a[i]={12,15,17,19,20,22,23,26,29,35,40,51};

用值21调用lower_bound(),返回一个指向22的iterator。用值22调用lower_bound(),也返回一个指向22的iterator。第一个版本使用底层 < (小于)操作符,第二个版本根据comp进行排序和比较。

注意事项

调用lower_bound之前必须确定序列为有序序列,否则调用出错。第一个版本排序根据底层的 <(小于)操作符,第二个版本根据comp进行排序。

举例

编辑

第一个版本

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

vector<int>nums;

nums.push_back(-242);

nums.push_back(-1);

nums.push_back(0);

nums.push_back(5);

nums.push_back(8);

nums.push_back(8);

nums.push_back(11);

cout<<"Beforenumsis:";

for(unsignedinti=0;i<nums.size();i++){

cout<<nums[i]<<"";

}

cout<<endl;

vector<int>::iteratorresult;

int new_val=7;

result=lower_bound(nums.begin(),nums.end(),new_val);

nums.insert(result,new_val);

cout<<"After,numsis:";

for(unsignedinti=0;i<nums.size();i++){

cout<<nums[i]<<"";

}

cout<<endl;

输出:

Before nums is: -242 -1 0 5 8 8 11

After, nums is: -242 -1 0 5 7 8 8 11

第二个版本

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

#include<iostream>

#include<vector>

#include<algorithm>

using namespace std;

class person

{

private:

    int age;//年龄

    double height;//身高

    string name;//姓名

public:

    person():age(0),height(0.0)

    {}

    person(const int age_,

            const double height_,

            const string &name_)

            :age(age_),

            height(height_),

            name(name_) {}

    int key1() const

    {

        return age;

    }

    double key2() const

    {

        return height;

    }

    const string &key3() const

    {

        return name;

    }

};

struct compareByTwoKey

{

    template<class T1,class T2>

    int operator()(const T1 &t1,const T2 &t2)

    {

        if(t1.key1()<t2.key1())

            return true;

        else

            if(t1.key1()==t2.key1())

            {

                if(t1.key2()<t2.key2())

                    return true;

                else

                    return false;

            }

            else

                return false;

    }

};

void init_data(vector<person> &v)

{

    v.push_back(person(12,96.8,"你好"));

    v.push_back(person(12,86.9,"你好"));

    v.push_back(person(13,76.8,"你好"));

    v.push_back(person(13,77.8,"你好"));

    v.push_back(person(10,70.8,"你好"));

    v.push_back(person(15,79.8,"你好"));

    v.push_back(person(18,176.8,"你好"));

    v.push_back(person(2,6.8,"你好"));

    v.push_back(person(12,55.8,"你好"));

    v.push_back(person(12,97.8,"hello"));

    sort(v.begin(),v.end(),compareByTwoKey());//一定要加上排序,并且排序函数跟lower_bound一致

}

int main()

{

    vector<person> v;

    init_data(v);

    personval(12,90.0,"hello");

    vector<person>::iteratoriter iter=lower_bound(v.begin(),v.end(),val,compareByTwoKey());//跟sort函数一致

    system("pause");

}

iter指向person(12, 96.8, "你好")这一条记录。读者也可以更改compareByTwoKeycompareByThreeKey

发布了339 篇原创文章 · 获赞 46 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/u014644594/article/details/103859440