排序算法merge合并

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Print
{
public:
   void operator()(int i)
   {
      cout << i << endl;
   }
};

int main()
{
   vector<int> v1;
   vector<int> v2;
   for(int i = 0; i < 10; i++)
   {
      if(i % 2 == 0)
      {
         v1.push_back(i);
      }
      else
      {
         v2.push_back(i);
      }
   }

   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(), Print());
   
   return 0;
}
$ ./a.out      
0
1
2
3
4
5
6
7
8
9

注:合并的两个容器必须是有序序列

posted @ 2022-07-31 16:01  thomas_blog  阅读(31)  评论(0编辑  收藏  举报