C++ algorithm中常用函数——判断容器中是否包含某个值

假设定义类

class Student
{
public:
    Student(string name, int age) :m_name(name), m_age(age) {}
    bool operator==(const Student &obj) const
    {
        return this->m_age == obj.m_age;
    }

    bool operator<(const Student &obj) const
    {
        return this->m_age < obj.m_age;
    }

    string GetName() const
    {
        return m_name;
    }

    int GetAge() const
    {
        return m_age;
    }

private:
    string m_name;
    int m_age;
};

 

 

1. adjacent_find();

eg:vector::iterator res = adjacent_find(source.begin(), source.end()); 相邻元素相等

vector::iterator res = adjacent_find(source.begin(), source.end(),custom_func); //相邻元素符合某一条件

 vector::iterator  it3 = std::adjacent_find(students.begin(), students.end(), [](Student s1, Student s2) {return s1.GetAge() == (s2.GetAge() - 3); });

得到容器中满足条件的相邻元素,并返回第一个元素位置。

2.binary_search();

 std::sort(students.begin(), students.end(), [](Student s1, Student s2) {return s1.GetAge() < s2.GetAge(); }); 先排序,从小到大
 bool isExist = std::binary_search(students.begin(), students.end(), Student("pp", 12));

返回值是布尔类型,判断是否存在,存在返回true

3.count();

int number = std::count(source.begin(), source.end(), 8);

如果容器存储的是类或结构体,要在类里面添加 operator==,否则编译出错

    bool operator==(Person obj)
    {
        return this->m_age == obj.m_age;
    }

4.count_if();

eg: int res = count_if(source.begin(), source.end(), [](int x) {return x > 4; });

得到容器中满足条件元素的数量

5. find_end();

eg:  std::vector::iterator it = std::find_end(v1.begin(), v1.end(), v2.begin(), v2.end());

v1中的元素倒叙遍历,若元素不在v2中,返回值

  if (it != v1.end())
        std::cout << "v1last found at position " << (it - v1.begin()) << '\n';

使用时注意添加判断,另外常规类型可以,自定义类型使用失败。

6.find();

eg:  vector::iterator it2 = std::find(students.begin(), students.end(),Student("pp",12));

寻找某个容器中符合条件的第一个位置

7.find_end();

eg:

    vector students;
    students.push_back(Student("A", 1));
    students.push_back(Student("B", 2));
    students.push_back(Student("C", 10));
    students.push_back(Student("D", 1));
    students.push_back(Student("E", 2));
    students.push_back(Student("F", 15));

    vector students2;
    students2.push_back(Student("C", 1));
    students2.push_back(Student("D", 2));
    vector::iterator it = std::find_end(students.begin(), students.end(), students2.begin(), students2.end());

遍历容器student,返回最后一个等于student2位置,没有返回student.end()

8.find_first_of();

eg:vector::iterator it = std::find_first_of(students.begin(), students.end(), students2.begin(), students2.end());

遍历容器student,返回第一个等于student2位置,没有返回student.end()

9.find_if();

eg  vector::iterator it = std::find_if(students.begin(), students.end(), [](Student h1) {return h1.GetAge() == 10; });

返回容器student中,第一个符合条件的元素位置

10.lower_bound();

eg  vector::iterator it6 = std::lower_bound(students.begin(), students.end(), Student("d", 10));

先排序,然后返回第一个符合的元素,查找失败返回student.end();

11.upper_bound();

eg   vector::iterator it = std::upper_bound(students.begin(), students.end(), Student("d", 15));

先排序,然后返回最后一个符合的元素的下一个元素位置,查找失败返回student.end();如果是最后一个元素

直接返回student.end()

12 search();

eg vector::iterator it = std::search(students.begin(), students.end(), students2.begin(), students2.end());

遍历容器student,返回第一个等于student2位置,没有返回student.end()

13. search_n();

eg  vector::iterator it6 = std::search_n(students.begin(), students.end(), 2, Student("", 2), [](Student s1,Student s2) {return s1.GetAge() > s2.GetAge(); });

寻找 2次大于Student("", 2) 的第一个位置  ,大于由lambda表达式定义

14. std::set_difference() 求差值

#include 
#include 
#include 
#include 
 
int main() {
    std::vector v1 {1, 2, 5, 5, 5, 9};  //必须排序,否则报错
    std::vector v2 {2, 5, 7};
    std::vector diff;
 
    std::set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), 
                        std::inserter(diff, diff.begin()));
 
    for (auto i : v1) std::cout << i << ' ';
    std::cout << "minus ";
    for (auto i : v2) std::cout << i << ' ';
    std::cout << "is: ";
    for (auto i : diff) std::cout << i << ' ';
    std::cout << '\n';
}

 

 

你可能感兴趣的:(C++ algorithm中常用函数——判断容器中是否包含某个值)