//std::merge的两个版本

template<class InputIt1, class InputIt2, class OutputIt>  //First version
OutputIt merge(InputIt1 first1, InputIt1 last1,           //指向一个新容器的迭代器d_first
               InputIt2 first2, InputIt2 last2,
               OutputIt d_first)
{
    for (; first1 != last1; ++d_first) {//如果第一个容器为空,则执行return std::copy(first2,last2,d_first);
        if (first2 == last2) {
            return std::copy(first1, last1, d_first);     //如果第二个容器为空,则执行return std::copy(first1,last1,d_first);
        }
        if (*first2 < *first1) {                          //这个如果第二个迭代器指向的第二个容器的元素为空,那么是不是重载的迭代器返回的值为0呢
            *d_first = *first2;
            ++first2;
        } else {
            *d_first = *first1;                           //对两个已经排好序的容器的操作,应用了归并算法,
            ++first1;                                      //最后指向++first1时的前提是*first1<*first2;因此才能执行最后的return;
        }
    }
    return std::copy(first2, last2, d_first);
}

template<class InputIt1, class InputIt2,                  // Second version
         class OutputIt, class Compare>
OutputIt merge(InputIt1 first1, InputIt1 last1,           //指向一个新容器的迭代器d_first
               InputIt2 first2, InputIt2 last2,
               OutputIt d_first, Compare comp)
{
    for (; first1 != last1; ++d_first) {
        if (first2 == last2) {
            return std::copy(first1, last1, d_first);
        }
        if (comp(*first2, *first1)) {
            *d_first = *first2;
            ++first2;
        } else {
            *d_first = *first1;
            ++first1;
        }
    }
    return std::copy(first2, last2, d_first);
}

//std::copy的实现
template<class InputIt, class OutputIt>      //First version
OutputIt copy(InputIt first, InputIt last,
              OutputIt d_first)
{
    while (first != last) {
        *d_first++ = *first++;
    }
    return d_first;
}

template<class InputIt, class OutputIt, class UnaryPredicate>   //Sencond version
OutputIt copy_if(InputIt first, InputIt last,
                 OutputIt d_first, UnaryPredicate pred)
{
    while (first != last) {
        if(pred(*first))                                        //意图何在?函数指针
            *d_first++ = *first;
         first++;
    }
    return d_first;
}

STL merge的实现细节的更多相关文章

  1. STL - merge()

    merge用来对两个有序容器进行合并.返回合并后存入容器中的元素的下一个位置的迭代器(可以认为是超尾). merge(v1.first(),v1.end(),v2.first(),v2.end(),r ...

  2. Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序

    A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  3. C++ STL编程轻松入门基础

    C++ STL编程轻松入门基础 1 初识STL:解答一些疑问 1.1 一个最关心的问题:什么是STL 1.2 追根溯源:STL的历史 1.3 千丝万缕的联系 1.4 STL的不同实现版本 2 牛刀小试 ...

  4. CDQ分治学习笔记

    数据结构中的一块内容:$CDQ$分治算法. $CDQ$显然是一个人的名字,陈丹琪(NOI2008金牌女选手) 这种离线分治算法被算法界称为"cdq分治" 我们知道,一个动态的问题一 ...

  5. A*算法–A* algorithm tutorial

    Author:Justin Heyes-Jones From: http://heyes-jones.com/astar.php Date:2014.8.16 本文地址:http://www.cnbl ...

  6. SAP Cloud for Customer Extensibility的设计与实现

    今天的文章来自Jerry的同事,SAP成都研究院C4C开发团队的开发人员徐欢(Xu Boris).徐欢就坐我左手边的位置,因此我工作中但凡遇到C4C的技术问题,一扭头就可以请教他了,非常方便.下图是他 ...

  7. 冲刺Noip2017模拟赛6 解题报告——五十岚芒果酱

    1.ksum(ksum) [问题描述] Peter喜欢玩数组.NOIP这天,他从Jason手里得到了大小为n的一个正整数 数组. Peter求出了这个数组的所有子段和,并将这n(n+)/2个数降序排序 ...

  8. Sentry 监控 - Snuba 数据中台架构(编写和测试 Snuba 查询)

    系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - 创建版本 快速使用 Docker 上手 Sentry-CLI - 30 秒上手 Source Maps Sentry For ...

  9. STL源代码分析——STL算法merge合并算法

    前言 因为在前文的<STL算法剖析>中.源代码剖析许多.不方便学习.也不方便以后复习,这里把这些算法进行归类.对他们单独的源代码剖析进行解说.本文介绍的STL算法中的merge合并算法. ...

随机推荐

  1. leetcode -- Search for a Range (TODO)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  2. C#并行编程 (Barrier,CountdownEvent,ManualResetEventSlim,SemaphoreSlim,SpinLock,SpinWait )

    背景 有时候必须访问变量.实例.方法.属性或者结构体,而这些并没有准备好用于并发访问,或者有时候需要执行部分代码,而这些代码必须单独运行,这是不得不通过将任务分解的方式让它们独立运行. 当任务和线程要 ...

  3. SQL Server日期函数总结

    获得一个月的天数:首先到得一个月最后一天的日期,通过 SQL Server 日期函数 day() 取得日期中的“天 ”部分 获得 2008 年 2 月份的天数:select day(cast('200 ...

  4. 最简单的Java调用C/C++代码的步骤

    1)首先在Java类中声明一个native的方法 (2)使用javah命令生成包含native方法声明的C/C++头文件 (3)按照生成的C/C++头文件来写C/C++源文件 (4)将C/C++源文件 ...

  5. eclipse ctrl+左击不能关联相应文件

    <?xml version="1.0" encoding="UTF-8"?><projectDescription> <name& ...

  6. Bitmap 多语言实现及应用

    http://blog.studygolang.com/2014/09/bitmap_multi_language/

  7. mysql查询更新时的锁表机制分析

    为了给高并发情况下的mysql进行更好的优化,有必要了解一下mysql查询更新时的锁表机制. 一.概述 MySQL有三种锁的级别:页级.表级.行级.MyISAM和MEMORY存储引擎采用的是表级锁(t ...

  8. hdu 4417 划分树

    思路:二分枚举区间第k大.用划分树查找是否符合要求的高度. #include<iostream> #include<algorithm> #include<cstdio& ...

  9. hdu 4340 树状DP

    思路:我们定义两个数组,ant[Maxn][2],bob[Maxn][2].ant[i][0]表示还未确定哪个城市被全费用占领,ant[i][1]表示确定了哪个城市被全费用占领.那么ant[i][0] ...

  10. hdu 4280 网络流

    裸的网络流,递归的dinic会爆栈,在第一行加一句就行了 #pragma comment(linker, "/STACK:1024000000,1024000000") #incl ...