C++ STL Merge的用法

merge函数的作用是:将两个有序的序列合并为一个有序的序列。函数参数:merge(first1,last1,first2,last2,result,compare);

//firs1t为第一个容器的首迭代器,last1为第一个容器的末迭代器,first2为第二个容器的首迭代器,last2为容器的末迭代器,result为存放结果的容器,comapre为比较函数(可略写,默认为合并为一个升序序列)。

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

你可能感兴趣的:(c++)