合并两个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 ...
随机推荐
- jQuery $.each()常见的几种使用方法
<code class="language-html"><!doctype html> <html> <head> <meta ...
- 插入排序算法 Java实现
插入排序算法是算法排序中的一种: 该算法是假设已有序列是有序序列,从首元素(首元素为单个元素,肯定是有序的...)开始分析,对其他元素的位置进行有序的确定: 以算法为例: public class I ...
- 基于Scrapy-Redis和docker技术在单机上构建分布式爬虫
准备工作: 安装docker https://www.docker.com/docker-ubuntu 选择ce版本 通过docker pull 下载基础镜像,ubuntu16.04, redis, ...
- webpack进阶构建项目(一):1.理解webpack加载器
1.理解webpack加载器 webpack的设计理念,所有资源都是“模块”,webpack内部实现了一套资源加载机制,这与Requirejs.Sea.js.Browserify等实现有所不同. We ...
- shell脚本实例-跟踪网站日常变动
#!/usr/bin/bash #用途:跟踪网页是否有更新 if [ $# -ne 1 ];then echo -e "$Usage $0 URl " exit fi first_ ...
- vue 缩水版 双向绑定
function Observer(obj, key, value){ var dep = new Dep(); if (Object.prototype.toString.call(value) = ...
- 解决java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor问题
hibernate整合spring当在spring配置文件中加入如下代码 <!--2.配置事务属性,需要事务管理器--> <tx:advice id="txAdvice&q ...
- 什么是lambda函数,它有什么好处
编程中提到的lambda表达式,通常是在需要一个函数,但是又不想费神去命名一个函数的场合下使用,也就是值匿名函数. python允许你定义一种单行的小函数.定义lambda函数的形式如下lambda参 ...
- winform 点击控件拖动窗体
private Point mPoint = new Point(); private void 选择控件_MouseDown(object sender, MouseEventArgs e) { m ...
- matlab调用规则变量名eval函数
eval 函数运用!! 经常会遇到matlab里面有些变量命名其实有一样的规律,,但是不像矩阵这些是可以通过循环来获取的,这个时候就可以利用eval语句了: 首先,假设现在有10个名称类似的变量, ...