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);
  1. 把两个已排序的ranges合并成单一的一个range,若两个input range有等价(相同)的元素,第一个range的排在第二个range的之前,稳定的,元素相对位置不改变 
  2. [first1,last1)和[result,result+(last1-first1)+(last2-first2))不重叠 ,合并后的长度等于两个range长度之和
  3. [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)的更多相关文章

  1. c++合并两个序列函数merge()和inplace_merge()

    大家在写归并排序时是不是觉得合并两个序列有点麻烦,有快速的方法吗? 我们全部函数自己写,比如: #include<bits/stdc++.h> using namespace std; # ...

  2. LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)

    21. 合并两个有序链表 21. Merge Two Sorted Lists 题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. LeetCode ...

  3. 高效合并两个有序数组(Merge Sorted Array)

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: Y ...

  4. [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 ...

  5. [Swift]LeetCode88. 合并两个有序数组 | Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

  6. LeetCode 21:合并两个有序链表 Merge Two Sorted Lists

    将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. Merge two sorted linked lists and return it as a new ...

  7. ADO:DataSet合并两张表( ds.Merge(ds1))

    原文发布时间为:2008-08-01 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...

  8. LeetCode 23. 合并K个排序链表(Merge Two Sorted Lists)

    23. 合并K个排序链表 23. Merge k Sorted Lists 题目描述 合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. LeetCode23. Merge k S ...

  9. Leetcode#88. Merge Sorted Array(合并两个有序数组)

    题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...

随机推荐

  1. String类的常用方法总结

    一.String类String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.java把String类声明的final类,不能有类.String类对象创建 ...

  2. Centos7扩展存储空间

    1.在关机状态下,在Vm里面设置系统大小到需要的数值 :2.使用root权限登陆linux,df -h查看系统情况: [root@cratedb-hk-01 ~]# df -h 文件系统 容量 已用 ...

  3. 使用 DirectX 创建 3D 图形

    官方链接   https://msdn.microsoft.com/zh-cn/library/windows/desktop/hh465137.aspx 使用 Windows 运行时初始化 Dire ...

  4. cf467D(map,vector,bfs,特点提取)

    D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. 玩转 React 【第03期】:邂逅 React 组件

    上期回顾 前文我们讲解了 React 模板 JSX,接着我们继续来看看 React 组件又是如何工作的呢? 组件化开发到了今天已经是大家的共识,在 React 中,组件同样也是组成我们整个项目的基本单 ...

  6. L264 how cats are psychopaths

    When Becky Evans started studying cat-human relationships, she kept hearing, over and over again, ab ...

  7. requery.js使用姿势

    最近在看requerjs,现在来总结下自己的收获,有不对的地方,望大家指正! 1.首先介绍下requirejs,引用中文官网http://www.requirejs.cn的一句话,requirejs是 ...

  8. vue 缩水版 双向绑定

    function Observer(obj, key, value){ var dep = new Dep(); if (Object.prototype.toString.call(value) = ...

  9. 引入网站标题小logo

    <link rel="icon" type="image/png" href="路径/favicon.png"> 关注微信小程序

  10. Python 栈和队列,双向队列

    # 栈 # 特点: 先进后出 class StackFullException(Exception): pass class StackEmptyException(Exception): pass ...