合并两个sorted ranges(merge和inplace_merge)
merge
//版本一:用operator <比较元素
template <class InputerIterator1,class InputerIterator2,class OutputIterator>
OutputIterator merge(InputerIterator1 first1,InputerIterator1 last1,
InputerIterator2 first2,InputerIterator2 last2,
OutputIterator result); //版本二:用自定义的function object比较元素
template <class InputerIterator1,class InputerIterator2,class OutputIterator,class StrictWeakOrdering>
OutputIterator merge(InputerIterator1 first1,InputerIterator1 last1,
InputerIterator2 first2,InputerIterator2 last2,
OutputIterator result,StrictWeakOrdering cmp);
- 把两个已排序的ranges合并成单一的一个range,若两个input range有等价(相同)的元素,第一个range的排在第二个range的之前,稳定的,元素相对位置不改变
- [first1,last1)和[result,result+(last1-first1)+(last2-first2))不重叠 ,合并后的长度等于两个range长度之和
- [first2,last2)和[result,result+(last1-first1)+(last2-first2))不重叠
inplace_merge
//版本一:用operator <比较元素
template <class BidirectionalIterator>
inline void inplace_merge(BidirectionalIterator first,BidirectionalIterator middle,BidirectionalIterator last); //版本二:用自定义的function object比较元素
template <class BidirectionalIterator,class StrictWeakOrdering>
inline void inplace_merge(BidirectionalIterator first,BidirectionalIterator middle,BidirectionalIterator last,StrictWeakOrdering cmp);
把两段已连接的分别排序的合成一个排序的range,两段递增的range合并成一个递增的range,range中的相对元素也不会改变,此算法会分配临时内存缓冲区,运行速度取决于多少内存可用
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std; int main()
{
vector<int> v{,,,,};
vector<int> v1{,,,,,};
merge(v.begin(),v.end(),v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
cout<<endl; vector<int> v2{,,,,,};
inplace_merge(v2.begin(),v2.begin()+,v2.end());
for_each(v2.begin(),v2.end(),[](int i)
{
cout<<i<<" ";
});
cout<<endl;
return ;
}
合并两个sorted ranges(merge和inplace_merge)的更多相关文章
- c++合并两个序列函数merge()和inplace_merge()
大家在写归并排序时是不是觉得合并两个序列有点麻烦,有快速的方法吗? 我们全部函数自己写,比如: #include<bits/stdc++.h> using namespace std; # ...
- LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)
21. 合并两个有序链表 21. Merge Two Sorted Lists 题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. LeetCode ...
- 高效合并两个有序数组(Merge Sorted Array)
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: Y ...
- [Swift]LeetCode21. 合并两个有序链表 | Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- [Swift]LeetCode88. 合并两个有序数组 | Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- LeetCode 21:合并两个有序链表 Merge Two Sorted Lists
将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. Merge two sorted linked lists and return it as a new ...
- ADO:DataSet合并两张表( ds.Merge(ds1))
原文发布时间为:2008-08-01 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- LeetCode 23. 合并K个排序链表(Merge Two Sorted Lists)
23. 合并K个排序链表 23. Merge k Sorted Lists 题目描述 合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. LeetCode23. Merge k S ...
- Leetcode#88. Merge Sorted Array(合并两个有序数组)
题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...
随机推荐
- L1-053 电子汪
据说汪星人的智商能达到人类 4 岁儿童的水平,更有些聪明汪会做加法计算.比如你在地上放两堆小球,分别有 1 只球和 2 只球,聪明汪就会用“汪!汪!汪!”表示 1 加 2 的结果是 3. 本题要求你为 ...
- Centos7部署hadoop 3
一:ssh免密登录: 1)vim /etc/ssh/sshd_config去掉注释或添加 RSAAuthentcation yes PubkeyAuthentication yes # Authent ...
- DevExpress WinForms使用教程:SVG图库和Image Picker
[DevExpress WinForms v18.2下载] 每个新版本都在几个新控件中引入了矢量图标支持. 对于v18.2,这是列表: BackstageViewControl及其项目 RecentI ...
- jmeter中测试接口
本文主要介绍在jmeter中测试接口:主要从以下几个方面进行说明: 1.jmeter简介 2.jmeter怎么做接口测试 3.jmeter进行参数化的几种形式 4.jmeter中处理乱码方法 5.jm ...
- VS2012里面使用EF框架的增删改查和分页的方法
public class BaseRepository<T> where T : class { //实例化EF框架 DataModelContainer ...
- 句柄线程做参数和PostMessage的用法
当我们启动一个线程,并且要给线程函数传递的参数是窗口句柄时,我们应该这样做: HWND hHwnd = GetSafeHwnd(); HANDLE hThread; DWORd dwThreadId; ...
- mysql储存引擎
Mysql数据库常用存储引擎 数据库存储引擎:是数据库底层软件组织,数据库管理系统(DBMS)使用数据引擎进行创建.查询.更新和删除数据.不同的存储引擎提供不同的存储机制.索引技巧.锁定水平等功能,使 ...
- python sub替换方法
命令:re.sub(pattern, repl, string, count=0, flags=0) re.sub 用于替换字符串的匹配项.如果没有匹配到规则,则原字符串不变. 第一个参数:规则 第二 ...
- Spring架构-01-微服务架构
一.单体架构 所有功能,所有模块都耦合在一个系统里面,如传统的一MVC. 需要重新编译测试,重新部署. 伸缩性差 可靠性差 系统迭代困难 跨开发语言程序低 团队协作麻烦 二.微服务架构 常见架构风格: ...
- 【转载】 996,谁的ICU?
原文地址: https://baijiahao.baidu.com/s?id=1629803937354992525&wfr=spider&for=pc --------------- ...