C++ STL Merge的用法

来源:互联网 发布:棋牌软件 编辑:程序博客网 时间:2024/06/03 11:18

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

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


#include "algostuff.hpp"
#include <iterator>
#include <ostream>
using namespace std;


int main(){
list<int> coll1;
set<int> coll2;


INSERT_ELEMENTS(coll1,1,6);
INSERT_ELEMENTS(coll2,3,8);


PRINT_ELEMENTS(coll1,"coll1:");
cout<<endl;
PRINT_ELEMENTS(coll2,"coll2:");
cout<<endl;
cout<<"merged:";


merge(coll1.begin(),coll1.end(),coll2.begin(),coll2.end(),ostream_iterator<int>(cout," "));


cout<<endl;


return 1;
}




输出:

coll1:1 2 3 4 5 6 
coll2:3 4 5 6 7 8 
merged:1 2 3 3 4 4 5 5 6 6 7 8 


0 0