merge原型:

std::merge

default (1)
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator merge (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result);
custom (2)
template <class InputIterator1, class InputIterator2,
class OutputIterator, class Compare>
OutputIterator merge (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp);

该函数是将两个范围内的元素合并到一个新的位置(result)中,而且保证有序。

使用operator<进行比較。

在使用该函数之前,应该保证两个子范围内的元素都是有序的!

result的大小为两个子范围元素个数之和。应保证result的大小足以容纳全部的元素。

返回值为result的最后一个被覆盖元素的下一个元素的迭代器。

其行为类似于:

template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator merge (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
while (true) {
if (first1==last1) return std::copy(first2,last2,result);
if (first2==last2) return std::copy(first1,last1,result);
*result++ = (*first2<*first1)? *first2++ : *first1++;
}
}

一个简单的样例:(result本身为空)

#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
using namespace std;
void merge2(){
vector<int> vi{1,3,5,7,9};
array<double,4> ad{2.0,4.0,6.0,8.0};
cout<<"vi=";
for(int i:vi)
cout<<i<<" ";
cout<<endl;
cout<<"ad=";
for(double i:ad)
cout<<i<<" ";
cout<<endl; vector<double> vr(vi.size()+4);
auto it=merge(vi.begin(),vi.end(),ad.begin(),ad.end(),vr.begin());
cout<<"vr=merge(vi.begin(),vi.end(),ad.begin(),ad.end(),vr.begin())\nvr=";
for(double i:vr)
cout<<i<<" ";
cout<<endl; if(it==vr.end())
cout<<"merge(vi.begin(),vi.end(),ad.begin(),ad.end(),vr)=vr.end()!"<<endl; }

执行截图:

result本身不为空的时候:

#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
using namespace std;
void merge3(){
vector<int> vi{1,3,5,7,9};
array<double,4> ad{2.0,4.0,6.0,8.0};
cout<<"vi=";
for(int i:vi)
cout<<i<<" ";
cout<<endl;
cout<<"ad=";
for(double i:ad)
cout<<i<<" ";
cout<<endl; vector<double> vr{11,22,33,44,55,66,77,88,99,111,222,333};
for(double i:vr)
cout<<i<<" ";
cout<<endl;
auto it=merge(vi.begin(),vi.end(),ad.begin(),ad.end(),vr.begin());
cout<<"after merge(vi.begin(),vi.end(),ad.begin(),ad.end(),vr.begin())\nvr=";
for(double i:vr)
cout<<i<<" ";
cout<<endl; if(it==vr.end())
cout<<"merge(vi.begin(),vi.end(),ad.begin(),ad.end(),vr)=vr.end()!"<<endl;
else
cout<<"it="<<*it<<endl; }

执行截图:



能够看到,这样的情况下返回的迭代器指向111,也就是最后一个被覆盖的元素的下一个!

——————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,能够在以下留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我改动,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

2014-9-17

于GDUT

——————————————————————————————————————————————————————————————————


STL algorithm算法merge(34)的更多相关文章

  1. STL algorithm算法mismatch(37)

    mismatch原型: std::mismatch equality (1) template <class InputIterator1, class InputIterator2> p ...

  2. STL algorithm算法is_permutation(27)

    is_permutation原型: std::is_permutation equality (1) template <class ForwardIterator1, class Forwar ...

  3. STL algorithm算法lower_bound和upper_bound(31)

    lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...

  4. STL algorithm算法minmax,minmax_element(36)

    minmax原型: std::minmax C++11 C++14 default (1) template <class T> pair <const T&,const T ...

  5. STL algorithm算法min,min_element(35)

    min样板: std::min C++98 C++11 C++14 default (1) template <class T> const T& min (const T& ...

  6. STL algorithm算法max,max_elements(33)

    max原型: std::max C++98 C++11 C++14 default (1) template <class T> const T& max (const T& ...

  7. STL algorithm算法mov,move_backward(38)

    move原型: std::move template <class InputIterator, class OutputIterator> OutputIterator move (In ...

  8. STL algorithm算法make_heap和sort_heap(32)

    make_heap原型: std::make_heap default (1) template <class RandomAccessIterator> void make_heap ( ...

  9. STL algorithm算法lexicographical_compare(30)

    lexicographical_compare原型: std::lexicographical_compare default (1) template <class InputIterator ...

随机推荐

  1. UIImageView 的 contentMode

    UIViewContentModeScaleToFill, // 按设置尺寸 - 填充 UIViewContentModeScaleAspectFit, // 按设置尺寸 - 等比例填充, 有边界 U ...

  2. Aircrack-ng官方文档翻译[中英对照]---Airmon-ng

    Aircrack-ng官方文档翻译---Airmon-ng Description[简介] This script can be used to enable monitor mode on wire ...

  3. apache开启.htaccess及.htaccess的使用方法

    今天本地调试PHP程序,用到了.htaccess,而默认配置里面开启.htaccess,在网上找到了开启.htaccess的可行方法,供朋友们借鉴.(开启的我他的方法不行,查找了一下AllowOver ...

  4. [原博客] BZOJ 2725 : [Violet 6]故乡的梦

    这个题在bzoj上好像是个权限题,想做的可以去Vani的博客下载测试数据.这里有题面. 简单叙述一下题意:给你一个n个点.m条边的带权无向图,S点和T点,询问Q次删一条给定的边的S-T最短路. 其中  ...

  5. IAR Embedded Workbench 破解方法+工具+授权文件

    转自IAR Embedded Workbench 破解方法+工具+授权文件 本文重点阐述了如何手动爆破 IAR EWARM 6.x以及生成License,目的一是和大家分享下,二是自己记录下过程,以便 ...

  6. applicationDefaultJvmArgs:

    server.context-path=/HelloMultiServlet server.port=8080 applicationDefaultJvmArgs: [ "-agentlib ...

  7. 水平/竖直居中在旧版Safari上的bug

    今天调了两个出现在旧版Safari上的layout bug. 它们最初是在同事的iPad上被发现的, 我在自己桌面上安装的Safari 5.1.7上也能够复现. Bug1: .vertical-cen ...

  8. 制作LOGO的35种方法

    A logo design is really a graphical element (ideogram, symbol, emblem, icon, sign) that, along with ...

  9. 初学acm感想

    初学acm,觉得大部分题对我来说都是陌生的,好多类型没见过,好多题没思路,打击确实不小,或许这个阶段正是比较能考验人的时候吧,因为只有坚持下来才有收获,没有人生下来就是大神,所以不能气馁更不能放弃,有 ...

  10. 将批量下载的博客导入到手机后,通过豆约翰博客阅读器APP(Android手机)进行浏览,白字黑底,保护眼睛,图文并茂。

    首先下面演示的博文来自于以下地址:http://www.douban.com/note/423939291/ 需要先通过博客备份专家将导出的博文导入到手机(还不会用的朋友请先阅读http://www. ...