//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. ORACLE触发器具体解释

    ORACLE PL/SQL编程之八: 把触发器说透 本篇主要内容例如以下: 8.1 触发器类型 8.1.1 DML触发器 8.1.2 替代触发器 8.1.3 系统触发器 8.2 创建触发器 8.2.1 ...

  2. 使用asp.net动态添加html元素

    HtmlGenericControl gen = new HtmlGenericControl("div");        gen.InnerText = "HtmlG ...

  3. graylog2 架构--转载

    原文地址:http://docs.graylog.org/en/latest/pages/architecture.html Architectural considerations There ar ...

  4. Spark之路 --- Scala用JFreeChart画图表实例

    JFreeChart介绍 JFreeChart是JAVA平台上的一个开放的图表绘制类库.它完全使用JAVA语言编写,是为applications, applets, servlets 以及JSP等使用 ...

  5. 中兴电信光纤猫F612管理员密码获取方法

    1.telnet 192.168.1.1 账号:root 密码:Zte521 2.输入命令: sendcmd 1 DB p DevAuthInfo 得到管理员账号密码如下: <DM name=& ...

  6. java调用.net asmx服务

    有时候,在java下开发会调用一下.net下写的服务,看网上有各种方法,但总是不成功,总结下自己测试过能调用成功的方式: 1. 原始方式http-soap public static String p ...

  7. hdu 3473 裸的划分树

    思路: 用Sum[dep][i]记录从tree[po].l到i中进入左子树的和. #include<iostream> #include<algorithm> #include ...

  8. 【转载】常用Maven插件介绍

    http://www.cnblogs.com/crazy-fox/archive/2012/02/09/2343722.html 我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构 ...

  9. C#算法基础之插入排序

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. ajax-典型应用-验证用户名

    用servlet实现后台: