STL-- algorithm

Most of the following excerpt from "C ++ Standard Library"

STL provides standard algorithms, including searching, sorting, copying, reordering, modifying, and other numerical operations. Algorithms are not members of the class of vessel function, but a global function used with iterators.

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

using namespace std;

int main() {
    vector<int> coll;
    vector<int>::iterator pos;
    //coll.push_back(2);
    coll.push_back(5);
    coll.push_back(4);
    coll.push_back(1);
    coll.push_back(6);
    coll.push_back(3);

    pos = min_element(coll.begin(),coll.end());    //寻找最小值
    cout << "min:" << *pos << endl;
    pos = max_element(coll.begin(), coll.end());//寻找最大值
    cout << "max:" << *pos << endl;

    sort(coll.begin(),coll.end());  //排序
    cout << "sort:";
    for (pos = coll.begin(); pos != coll.end(); ++pos) {
        << << * POS COUT '';
    }
    cout << endl; // this is output. 4. 5 3 2. 6. 1 
    POS = Find (coll.begin (), coll.end (), 3); // find the value of the first element 3 
    cout << "the Find:"; 
    cout << endl << * pos;    

    reverse (pos, coll.end ()); // reverse pos and all the elements of the future, because the front position 3 pos know, so this operation is 3 after an element inverted to the foregoing find () is used to aid the fight 
    
    for (POS = coll.begin ();! coll.end POS = (); POS ++) { 
        COUT POS << << * ''; 
    } 
    COUT << endl; 
    System ( "PAUSE"); 
}

Final output:

min:1
max:6
sort:1 3 4 5 6
find:3
1 6 5 4 3

 

Source: Zhangjiagang site optimization

Guess you like

Origin www.cnblogs.com/1994july/p/12152740.html