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 join()方法
描述 Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串. 语法 join()方法语法: str.join(sequence) 参数 sequence -- 要连接的 ...
- csu 10月 月赛 I 题 The Contest
Description 殷犇有很多队员.他们都认为自己是最强的,于是,一场比赛开始了~ 于是安叔主办了一场比赛,比赛有n个题目,每个题目都有一个价值Pi和相对能力消耗Wi,但是有些题目因为太坑不能同时 ...
- JAVA与编译语言及解释语言的关系
转自JAVA结合了编译和解释执行的优点 编译型语言是一次性编译成机器码,脱离开发环境独立运行,所以运行效率较高,但是由于编译成的是特定平台上机器码,所以可移植性差. 编译型语言的典型代表有C.C++. ...
- ASP.NET之HttpModule拦截404异常
Httpmodule代码: public class Error404Module : IHttpModule { public void Init(HttpApplication context) ...
- VS(Microsoft Visual Studio2010)工具打开项目所需的应用程序,出现未安装(.csproj)的应用程序的解决办法
打开这个文件的话,从列表中选择打开方式的时候,看看选择打开项目文件的打开方式是不是visual studio,如果是Micrisoft visual studio version selector.那 ...
- ARC模式下的单例写法
// 单例 + (id)sharedInstance { __strong static id sharedObject = nil; static dispatch_once_t onceToken ...
- Android-获取外置SDcard路径
Android手机支持SDcard.目前很多手机厂商把SDcard集成到手机中,当然有的手机同时也支持可插拔的SDcard.这就有了内置SDcard和位置SDcard之分.当手机同时支持内置和外置SD ...
- Android4.0 -- UI控件之 Menu 菜单的的使用(三)
上一讲 [Android 开发]:UI控件之 Menu 菜单的的使用(二) 我们讲解了创建上下文菜单的第一种使用方式:Creating a floating context menu [创建悬浮的上下 ...
- SQL Server数据库存在判断语句及系统表简介 转
Transact-SQL Exists Sentences--判断数据库是否存在IF EXISTS(SELECT * FROM master.sysdatabases WHERE name=N'库名' ...
- HDU-4300 Clairewd’s message
http://acm.hdu.edu.cn/showproblem.php?pid=4300 很难懂题意.... Clairewd’s message Time Limit: 2000/1000 MS ...