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. APNs推送, 处理通知

    设备接到apns发来的通知,应用处理通知有以下几种情况: 1. 应用还没有加载 这时如果点击通知的显示按钮,会调用didFinishLaunchingWithOptions,不会调用didReceiv ...

  2. 避免eclipse下启动run就进入debug模式

    分析原因:可能是eclipse的一个bug 解决方法:进入手机开发者模式设置,关闭usb调试和开发者模式,再重新打开即可.

  3. bootstrap兼容IE8 解决办法

    bootstrap有使用CSS3,所以ie9以下浏览器不会很顺畅.IE9以下的兼容性不是很好.IE本身就是怪胎,就搞一些和外界标准不一致.搞得web开发考虑这考虑那的兼容性,蛋疼! 基本上css3的没 ...

  4. logger.debug,logger.info,logger.warn,logger.error,logger.fatal的区别

    logger.debug,logger.info,logger.warn,logger.error,logger.fatal的区别 logger.debug,logger.info,logger.wa ...

  5. uva 11168 - Airport

    凸包+一点直线的知识: #include <cstdio> #include <cmath> #include <cstring> #include <alg ...

  6. GIT在LINUX下的基本操作

    没办法,看来,VIM技能也要同步练起来了. 离开了WIN的日常应用安乐窝,外面的世界有多精彩? GIT的错了我再改..呵呵 git clone http://username@1.2.3.4/repo ...

  7. PCB快速打样规范

    基本情况 板材为FR-4,板厚1.6mm    板材铜厚为1/2oz,成品铜厚为1oz(加工过程中的沉铜工艺会让铜层增加厚度) 绿油白字    喷锡工艺    最小孔内铜厚1.27um    电铜18 ...

  8. Qt之自定义插件(for Qt Designer)

    之前Blog里面有关于QWT的编译.配置.使用的文章,分别是在VS与Creator下进行的. QWT编译.配置.使用(VS2010 + Qt5.1.0). QWT编译.配置.使用(Qt Creator ...

  9. jdk jre jvm 关系

    很多朋友可能跟我一样,已经使用JAVA开发很久了,可是对JDK,JRE,JVM这三者的联系与区别,一直都是模模糊糊的. 今天特写此文,来整理下三者的关系. JDK : Java Development ...

  10. WordPress Events Manager插件多个跨站脚本漏洞

    漏洞名称: WordPress Events Manager插件多个跨站脚本漏洞 CNNVD编号: CNNVD-201310-196 发布时间: 2013-10-15 更新时间: 2013-10-15 ...