C++STL commonly used algorithms

C++STL commonly used algorithms

Search algorithm

adjacent_find()

Within the range of the iterator pair identification elements, search for a pair of adjacent repeated elements, and return an iterator pointing to the first element of the pair of elements if found. Otherwise, it returns past-the-end.

    vector<int> vecInt;
    vecInt.push_back(1);
    vecInt.push_back(2);
    vecInt.push_back(3);
    vecInt.push_back(4);
    vecInt.push_back(5);
    vecInt.push_back(5);
    vector<int>::iterator it = adjacent_find(vecInt.begin(), vecInt.end());

Search for value in the ordered sequence, and return true if found. Note: It cannot be used in a disordered sequence.

    set<int> setInt;
    setInt.insert(2);
    setInt.insert(1);
    setInt.insert(8);
    setInt.insert(5);
    setInt.insert(9);
    bool bFind = binary_search(setInt.begin(),setInt.end(),8);

count()

Use the equal operator to compare the elements in the mark range with the input value and return the equal number.

   vector<int> vecInt;
    vecInt.push_back(8);
    vecInt.push_back(2);
    vecInt.push_back(1);
    vecInt.push_back(4);
    vecInt.push_back(8);
    vecInt.push_back(6);
    int iCount = count(vecInt.begin(),vecInt.end(),8);//iCount==2

count_if()

The count_if algorithm calculates the element range [first, last), and returns the number of elements that meet the conditions.

   vector<int> vecInt;
    vecInt.push_back(3);
    vecInt.push_back(3);
    vecInt.push_back(1);
    vecInt.push_back(4);
    vecInt.push_back(8);
    vecInt.push_back(9);
    int count = count_if(vecInt.begin(), vecInt.end(), evenNumber);//The even number is 2;

find

Use the equal operator of the underlying element to compare the element in the specified range with the input value. When it matches, the search ends and an iterator of the element is returned.

    vector<int> vecInt;
    vecInt.push_back(3);
    vecInt.push_back(3);
    vecInt.push_back(2);
    vecInt.push_back(4);
    vecInt.push_back(8);
    vecInt.push_back(9);
    vector<int>::iterator it = find(vecInt.begin(), vecInt.end(), 2);

equal_range:

Return a pair of iterators, the first represents lower_bound and the second represents upper_bound.

    set<int> setInt;
    for (int i=0; i<10; i++)
    {
        setInt.insert(i+1);
    }
    pair<set<int>::iterator, set<int>::iterator> mypair = setInt.equal_range(3);
    set<int>::iterator it1 = mypair.first;
    cout << "it1:" << *it1 << endl;//3//4
    set<int>::iterator it2 = mypair.second;
    cout << "it2:" << *it2 << endl;//4//4

Search algorithm

merge() The following is the sorting and general algorithm: provide element sorting strategy merge: merge two ordered sequences and store them in another sequence. For example: vecIntA, vecIntB, vecIntC are containers declared with vector<int>, vecIntA already contains 1,3,5,7,9 elements, vecIntB already contains 2,4,6,8 elements vecIntC.resize(9);//Expand capacity merge(vecIntA.begin(), vecIntA.end(), vecIntB.begin(), vecIntB.end(), vecIntC.begin()); At this time, vecIntC stores 1, 2, 3 in order ,4,5,6,7,8,9 nine elements

sort()

sort: rearrange the elements in the specified range in ascending order by default. If you want to change the sorting rules, you can enter a comparison function.

//Student ID comparison function
bool Compare(const Student &stuA,const Student &stuB)
{
    return (stuA.getID()<stuB.getID());
}
int main()
{
    vector<Student> vecStu;
    vecStu.push_back(Student(2,"赵彤"));
    vecStu.push_back(Student(1,"王伟"));
    vecStu.push_back(Student(4,"李明"));
    vecStu.push_back(Student(3,"张燕"));
    sort(vecStu.begin(),vecStu.end(),Compare);
    for (vector<Student>::iterator it = vecStu.begin();it!= vecStu.end();it++) {
        cout<<it->getName() <<endl;
    }
}

random_shuffle() random_shuffle: Randomly adjust the order of the elements in the specified range.

        srand(time(0));//Set random seed

        vector<int> vecInt;
        vecInt.push_back(1);
        vecInt.push_back(3);
        vecInt.push_back(5);
        vecInt.push_back(7);
        vecInt.push_back(9);

        string str("itcastitcast ");
    
        random_shuffle(vecInt.begin(), vecInt.end());//Randomly sort, the result is such as: 9,7,1,5,3
        random_shuffle(str.begin(), str.end());//Randomly sort, the result is such as: "itstcasticat"

reverse()

        vector<int> vecInt;
        vecInt.push_back(1);
        vecInt.push_back(3);
        vecInt.push_back(5);
        vecInt.push_back(7);
        vecInt.push_back(9);
        reverse(vecInt.begin(), vecInt.end());//{9,7,5,3,1}
Reference: https://cloud.tencent.com/developer/article/1437081 C++STL Common Algorithms-Cloud + Community-Tencent Cloud