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. SQL--数据库--基本操作

    SQL基本操作 基本操作:CRUD 将SQL的基本操作根据操作对象进行分类:库操作,表操作(字段),数据操作 库操作 对数据库的增删改查 新增数据库 基本语法Create database 数据库名字 ...

  2. Android:如何获取屏幕的宽高

    WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); DisplayMet ...

  3. golang并发ping主机

    利用了golang对高并发的良好支持,同目录下将ip每行一个写入pinglist.txt文件即可 其实这个功能用linux一条命令就能搞定: cat pinglist.txt | xargs -P 1 ...

  4. 使用scrapy爬取dota2贴吧数据并进行分析

    一直好奇贴吧里的小伙伴们在过去的时间里说的最多的词是什么,那我们就来抓取分析一下贴吧发文的标题内容,并提取分析一下,看看吧友们在说些什么. 首先我们使用scrapy对所有贴吧文章的标题进行抓取 scr ...

  5. RESTful Service API 常见问题解决方案

    REST 风格的优秀设计应该像下面这些: - GET /users 获取所有用户 - GET /users/1234 获取ID为1234的用户 - POST /users 创建一个新用户 - PUT ...

  6. Python学习笔记第十周

    目录: 一.基础概念 1.多进程 2.进程间通信 3.进程锁 4.进程池 5.协程 a) greenlet b) Gevent 6.论事件驱动与异步IO 7.IO多路复用 8.Python Selec ...

  7. thinkphp+redis实现秒杀功能(转)

    1,安装redis,根据自己的php版本安装对应的redis扩展(此步骤简单的描述一下) 1.1,安装 php_igbinary.dll,php_redis.dll扩展此处需要注意你的php版本如图: ...

  8. 【error】'isnan' was not declared in this scope

    error问题 'isnan' was not declared in this scope isnan在cmath中被取消宏定义: // These are possible macros impo ...

  9. 带列表写入文件出错先 json.dumps

    output = json.dumps(output, ensure_ascii=False).encode('utf-8')

  10. Arcgis API本地化

    ①将API文件夹复制到该目录下C:\Program Files\ArcGIS\Server\framework\runtime\tomcat\webapps ②打开API文件夹中的init.js文件( ...