STL algorithm算法merge(34)
merge原型:
std::merge
| default (1) |
template <class InputIterator1, class InputIterator2, class OutputIterator> |
|---|---|
| custom (2) |
template <class InputIterator1, class InputIterator2, |
该函数是将两个范围内的元素合并到一个新的位置(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)的更多相关文章
- STL algorithm算法mismatch(37)
mismatch原型: std::mismatch equality (1) template <class InputIterator1, class InputIterator2> p ...
- STL algorithm算法is_permutation(27)
is_permutation原型: std::is_permutation equality (1) template <class ForwardIterator1, class Forwar ...
- STL algorithm算法lower_bound和upper_bound(31)
lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...
- STL algorithm算法minmax,minmax_element(36)
minmax原型: std::minmax C++11 C++14 default (1) template <class T> pair <const T&,const T ...
- 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& ...
- 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& ...
- STL algorithm算法mov,move_backward(38)
move原型: std::move template <class InputIterator, class OutputIterator> OutputIterator move (In ...
- STL algorithm算法make_heap和sort_heap(32)
make_heap原型: std::make_heap default (1) template <class RandomAccessIterator> void make_heap ( ...
- STL algorithm算法lexicographical_compare(30)
lexicographical_compare原型: std::lexicographical_compare default (1) template <class InputIterator ...
随机推荐
- 练习PYTHON协程之GREENLET
STACKLESS就算了,了解一下原理即可. GREENLET,GEVENT,EVENTLET这些,比较好测试,还是都 撸一次,得个印象. 测试代码都是网上的大路货. from greenlet im ...
- C51 的编程规范
编程首要是要考虑程序的可行性,然后是可读性.可移植性.健壮性以及可测试性.这是总则.但是很多人忽略了可读性.可移植性和健壮性(可调试的方法可能歌不相同),这是不对的. 1.当项目比较大时,最好分模块编 ...
- MFC程序的启动过程——先全局对象theApp(第一入口),后WinMain(真正入口),会引爆pApp->InitInstance从而创建窗口(程序员入口)
原文出自:http://blog.csdn.net/yuvmen/article/details/5877271 了解MFC程序的启动过程,对于初学者来讲,了学习MFC很有帮助:对于不常用VC的人来说 ...
- 值得推荐的C/C++框架和库 very good
[本文系外部转贴,原文地址:http://coolshell.info/c/c++/2014/12/13/c-open-project.htm]留作存档 下次造轮子前先看看现有的轮子吧 值得学习的C语 ...
- [cocos2d]场景切换以及切换进度显示
本文主要分两个部分叙述,第一是场景切换,第二是场景切换的进度显示. 一.场景切换 参考learn-iphone-and-ipad-cocos2d-game-development 第五章内容 coco ...
- [译]GotW #1: Variable Initialization
原文地址:http://herbsutter.com/2013/05/09/gotw-1-solution/ 第一个问题强调的是要明白自己在写什么的重要性.下面有几行简单的代码--它们大多数之间都有区 ...
- IPV6 实现
看代码实现前,请先保证了解ipv6的概念,可以先看ipv6介绍一文.code extract . 在文件 net/ipv6/af_inet6.c 中包含了ipv6协议初始化的主函数. static i ...
- Linux协议栈函数调用流程
普通网络驱动程序中必须要调用的函数是eth_type_trans(略),然后向上递交sk_buff时调用netif_rx()(net/core/dev.c).其函数中主要几行 __skb_queue_ ...
- Learing WCF Chapter1 WCF Services
WCF ServicesWCF services are the new distributed boundary in an enterprise application—with an empha ...
- t-sql 中between and 的一种写法
t-sql 中between and 的一种写法: where GETDATE() BETWEEN BeginDateTime AND EndDateTime; BeginDateTime,EndDa ...