This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Common search algorithms

【 Introduction 】 :

  •     find; // Find elements 
  •     find_if; // Find elements by condition
  •     adjacent_find; // Find adjacent repeating elements
  •     binary_search; // Binary search
  •     count; // Count elements
  •     count_if; // Count elements by conditions

find

【 Function 】 :

The iterator that returns the specified element is not found. End ()Copy the code

Function prototype:

find(iterator begin,iterator end,value);

// begin begins the iterator // end ends the iterator // value the element to be searchedCopy the code

“Demo” :

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

class Person
{
public:
    Person(string name, int age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }
    
    / / overloaded = =
    bool operator= = (const Person &p)
    {
        if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
        {
            return true;
        }
        return false;
    }
public:

    string m_Name;

    int m_Age;
};

void test01(a)
{
    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }

    // Find if there is 5 in the container
    vector<int>::iterator it = find(v.begin(), v.end(), 5);
    if(it ! = v.end())
    {
        cout << "Found it." << *it << endl;
    }
    else
    {
        cout << "Not found"<< endl; }}void test02(a)
{
    vector<Person> v;
    Person p1("Zhang".20);
    Person p2("Bill".19);
    Person p3("Fifty".21);
    Person p4("Daisy".22);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    
    Person targetP("Fifty".21);
    vector<Person>::iterator it = find(v.begin(), v.end(), targetP);
    if(it ! = v.end())
    {
        cout << "Found it." << it->m_Name << "," << it->m_Age << endl;
    }
    else
    {
        cout << "Not found"<< endl; }}int main(a)
{
    test01(a);test02(a);return 0;
}
Copy the code

Summary: We use find to find the specified element in a container, and the return value is an iterator

find_if

【 Function 】 :

Find elements by conditionCopy the code

Function prototype:

find_if(iterator begin,iterator end,_Pred);

// begin begins iterator // end ends iterator // _Pred function or predicate (return bool functor)Copy the code

“Demo” :

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

// Built-in data types
class GreaterFive
{
public:
    bool operator(a)(int v)
    {
        return v > 5; }};void test01(a)
{
    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }

    vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
    if(it ! = v.end())
    {
        cout << "Look for numbers greater than 5:" << *it << endl;
    }
    else
    {
        cout << "Not found"<< endl; }}class Person
{
public:
    Person(string name, int age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }
public:
    string m_Name;
    int m_Age;
};

class Greater20
{
public:
    bool operator(a)(Person &p)
    {
        return p.m_Age > 20; }};void test02(a)
{

    Person p1("Zhang".20);
    Person p2("Bill".22);
    Person p3("Fifty".19);
    vector<Person> v;
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);

    vector<Person>::iterator it = find_if(v.begin(), v.end(), Greater20());
    if(it ! = v.end())
    {
        cout << "Looking for someone over 20:" << it->m_Name << "," << it->m_Age << endl;
    }
    else
    {
        cout << "Not found"<< endl; }}int main(a)
{
    test01(a);test02(a);return 0;
}
Copy the code

Summary: Find_if conditional lookup is more flexible and provides a function that can change different strategies

adjacent_find

【 Function 】 :

Find adjacent repeating elementsCopy the code

Function prototype:

adjacent_find(iterator begin,iterator end);

// Find adjacent repeating elements, return the iterator at the first position of adjacent elements // begin start iterator // end end iteratorCopy the code

“Demo” :

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
void test01(a)
{
    vector<int> v;
    v.push_back(0);
    v.push_back(1);
    v.push_back(2);
    v.push_back(2);
    v.push_back(3);
    v.push_back(3);

    // Find adjacent repeating elements
    vector<int>::iterator it = adjacent_find(v.begin(), v.end());
    if(it ! = v.end())
    {
        cout << "Duplicate elements found:" << *it << endl;
    }
    else
    {
        cout << "Duplicate element not found"<< endl; }}int main(a)
{
    test01(a);return 0;
}
Copy the code

Summary: Use the adjacent_find algorithm in THE STL to find adjacent repeating elements in an interview question

binary_search

【 Function 】 :

Finds whether the specified element existsCopy the code

Function prototype:

bool binary_search(iterator begin,iterator end,value);

// Find the specified element, return true, otherwise return false; // Note: ** cannot be used in ** unordered sequences ** // begin begins the iterator // end ends the iterator // value looks for the elementCopy the code

“Demo” :

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
void test01(a)
{

    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }

    // Binary search
    bool ret = binary_search(v.begin(), v.end(), 2);
    if (ret)
    {
        cout << "Found it." << endl;
    }
    else
    {
        cout << "Not found"<< endl; }}int main(a)
{
    test01(a);return 0;
}
Copy the code

Summary: Binary lookup is efficient, but the elements in the lookup container must be ordered

count

【 Function 】 :

Number of statistical elementsCopy the code

Function prototype:

count(iterator begin,iterator end,value);

// Count the number of occurrences of an element // begin Start iterator // end End iterator // value Count the elementCopy the code

“Demo” :

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

// Customize the data type
class Person
{
public:
    Person(string name, int age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }
    bool operator= = (const Person &p)
    {
        if (this->m_Age == p.m_Age)
        {
            return true;
        }
        return false;
    }

public:
    string m_Name;
    int m_Age;
};

void test01(a)
{
    vector<int> v;
    v.push_back(0);
    v.push_back(0);
    v.push_back(1);
    v.push_back(2);
    v.push_back(2);
    v.push_back(3);

    int num = count(v.begin(), v.end(), 2);
    cout << "2 appears :" << num << "Time" << endl;
}

void test02(a)
{
    Person p1("Zhang".20);
    Person p2("Bill".20);
    Person p3("Fifty".20);
    vector<Person> v;
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);

    Person pTarget("Xiao Ming".20);
    int num = count(v.begin(), v.end(), pTarget);
    cout << "20-year-olds have it." << num << "People" << endl;
}

int main(a)
{
    test01(a);test02(a);return 0;
}
Copy the code

Note: When the statistics are of a customized data type, overloaded operator== is required

count_if

【 Function 】 :

Count the number of elements by conditionCopy the code

Function prototype:

count_if(iterator begin,iterator end,_Pred);

// start iterator // end iterator // _Pred predicateCopy the code

“Demo” :

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

class GreaterFive
{
public:
    bool operator(a)(int v)
    {
        return v > 5; }};// Customize the data type
class Person
{
public:
    Person(string name, int age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }

public:
    string m_Name;
    int m_Age;
};
class AgeGreater20
{
public:
    bool operator(a)(Person &p)
    {
        return p.m_Age > 20; }};void test01(a)
{
    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }

    int num = count_if(v.begin(), v.end(), GreaterFive());
    cout << "The numbers greater than 5 are << num << "个" << endl;
}

void test02(a)
{
    Person p1("Zhang".20);
    Person p2("Bill".21);
    Person p3("Fifty".22);
    Person p4("Daisy".23);

    vector<Person> v;
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);

    int num = count_if(v.begin(), v.end(), AgeGreater20());
    cout << "Those over the age of 20 << num << "People" << endl;
}
int main(a)
{
    test01(a);test02(a);return 0;
}
Copy the code

Please feel free to discuss in the comments section. The nuggets will draw 100 nuggets in the comments section after the diggnation project. See the event article for details