zoukankan      html  css  js  c++  java
  • Merge sorted ranges

    Combines the elements in the sorted ranges [first1,last1) and [first2,last2), into a new range beginning at result with all its elements sorted.
    The elements are compared using operator< for the first version, and comp for the second.

    The elements in both ranges shall already be ordered according to this same criterion (operator< or comp). The resulting range is also sorted according to this.

      template <class  InputIterator1, class InputIterator2, class OutputIterator>
        OutputIterator merge(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result) {
            while (true) {
                if (first1==last1) return std::copy(first2,last2,result);
                if (first2==last2) return std::copy(first1,last1,result);
                *result++ = (*first2<*first1)? *first2++ : *first1++;
            }
        }
  • 相关阅读:
    额外的 string 操作
    vector 对象是如何增长的
    顺序容器操作
    容器库概览
    顺序容器概述
    特定容器算法
    泛型算法结构
    再探迭代器
    定制操作
    使用关联容器
  • 原文地址:https://www.cnblogs.com/iuyy/p/13952212.html
Copyright © 2011-2022 走看看