Usage of C++ STL Merge

The purpose of the merge function is to merge two ordered sequences into one ordered sequence. Function parameters: merge(first1,last1,first2,last2,result,compare);

//firsts1t is the first iterator of the first container, last1 is the last iterator of the first container, first2 is the first iterator of the second container, last2 is the last iterator of the container, and result is the container that stores the result, comapre is a comparison function (can be omitted, the default is to merge into an ascending sequence).

truct myPrint
{
    
    
    myPrint()
    {
    
    
    }

    void operator () (int i)
    {
    
    
        cout<<i<<" " ;
    }

};


void test01()
{
    
    
    vector<int> v1{
    
    1,3,5,7};//有序
    vector<int> v2{
    
    2,3,6,8,9};//有序

    vector<int> v3;
    v3.resize(v1.size()+v2.size());//调整容器大小

    merge(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin());

    for_each(v3.begin(),v3.end(),myPrint());

}

输出:
1 2 3 3 5 6 7 8 9

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324139983&siteId=291194637