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 ...
随机推荐
- 可以用来开发h5的软件小结
webStorm phoneGap notepad++ eclips text sublime dreamWeaver intellij idea 学习h5 需要掌握的 大块的知识 xhtml ja ...
- TCP/IP入门学习(2)---OSI分层
一.会话层以上的处理 1.表示层 将数据从主机特有的格式转换为网络标准传输格式.以此使得不同环境之间的通信成为可能. 2.会话层 即决定使用哪个连接或者哪种连接方式将数据发送出去.会话层也会在数首部添 ...
- DM8168 编译filesystem步骤
在板子跑起来之前,需要先编译好8168的文件系统.前提是已经设置好板子的类型等参数,详见<DM8168环境搭建> 1.进入<DVR_RDK_BASE>/dvr_rdk目录 ma ...
- Apache httpd.conf的翻译
本人初学,15年暑假翻译了一些,前几天翻译完,有机器翻译,也有自己翻译的内容,不准确之处请指出. --------------------------------------------------- ...
- bzoj 3283: 运算器 扩展Baby Step Giant Step && 快速阶乘
3283: 运算器 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 184 Solved: 59[Submit][Status][Discuss] D ...
- Dynamips/Dynagen模拟CISCO路由环境
今天将<网络互连技术>--路由,交换与远程访问实训教程的实验书拿出来了看了部门. 搭建了一个基于DYNAGEN的虚拟环境. 归纳一下大约步骤: ~~~~~~~~~~~~~~ 一,在WIND ...
- CAS单点登录配置[5]:测试与总结
终于要结束了... 测试 1 我们同时打开Tomcat6和Tomcat7,如果报错请修改. 打 开浏览器,输入http://fighting.com/Client1,进入CAS登录界面,这里我们先输入 ...
- 主线程中有多个handler的情况
工作中遇到了这么一种情况,有两个视图,都需要开启异步任务从服务器获取数据,每个view中创建一个Handler,注册到异步任务中去,当异步任务从服务器获取数据出错,或者出现io异常或者http协议异常 ...
- git撤销提交到remote的commit
Reseting remote to a certain commit Assuming that your branch is called master both here and remotel ...
- 汉企C#面向对象——继承
public class Shengwu { private string _Name; public string Name { get { return _Name; } set { _Name ...