STL源码剖析——STL算法之merge合并算法

前言

    由于在前文的《STL算法剖析》中,源码剖析非常多,不方便学习,也不方便以后复习,这里把这些算法进行归类,对他们单独的源码剖析进行讲解。本文介绍的STL算法中的merge合并算法。源码中介绍了函数mergeinplace_merge。并对这些函数的源码进行详细的剖析,并适当给出使用例子,具体详见下面源码剖析。

merge合并算法源码剖析

// merge, with and without an explicitly supplied comparison function.
//将两个已排序的区间[first1,last1)和区间[first2,last2)合并
/*
函数功能:Combines the elements in the sorted ranges [first1,last1) and [first2,last2), 
into a new range beginning at result with all its elements sorted.

函数原型:
default (1)	:版本一
	template <class InputIterator1, class InputIterator2, class OutputIterator>
	OutputIterator merge (InputIterator1 first1, InputIterator1 last1,
                        InputIterator2 first2, InputIterator2 last2,
                        OutputIterator result);
custom (2)	:版本二
	template <class InputIterator1, class InputIterator2,
          class OutputIterator, class Compare>
	OutputIterator merge (InputIterator1 first1, InputIterator1 last1,
                        InputIterator2 first2, InputIterator2 last2,
                        OutputIterator result, Compare comp);
*/
//版本一:
template <class _InputIter1, class _InputIter2, class _OutputIter>
_OutputIter merge(_InputIter1 __first1, _InputIter1 __last1,
                  _InputIter2 __first2, _InputIter2 __last2,
                  _OutputIter __result) {
  __STL_REQUIRES(_InputIter1, _InputIterator);
  __STL_REQUIRES(_InputIter2, _InputIterator);
  __STL_REQUIRES(_OutputIter, _OutputIterator);
  __STL_REQUIRES_SAME_TYPE(
          typename iterator_traits<_InputIter1>::value_type,
          typename iterator_traits<_InputIter2>::value_type);
  __STL_REQUIRES(typename iterator_traits<_InputIter1>::value_type,
                 _LessThanComparable);
  //两个序列都尚未到达尾端,则执行while循环
  /*
  情况1:若序列二元素较小,则记录到目标区,且移动序列二的迭代器,但是序列一的迭代器不变.
  情况2:若序列一元素较小或相等,则记录到目标区,且移动序列一的迭代器,但是序列二的迭代器不变.
  最后:把剩余元素的序列复制到目标区
  */
  while (__first1 != __last1 && __first2 != __last2) {
	  //情况1
    if (*__first2 < *__first1) {//若序列二元素较小
      *__result = *__first2;//将元素记录到目标区
      ++__first2;//移动迭代器
    }
	//情况2
    else {//若序列一元素较小或相等
      *__result = *__first1;//将元素记录到目标区
      ++__first1;//移动迭代器
    }
    ++__result;//更新目标区位置,以便下次记录数据
  }
  //若有序列到达尾端,则把没到达尾端的序列剩余元素复制到目标区
  //此时,区间[first1,last1)和区间[first2,last2)至少一个必定为空
  return copy(__first2, __last2, copy(__first1, __last1, __result));
}
//版本二
template <class _InputIter1, class _InputIter2, class _OutputIter,
          class _Compare>
_OutputIter merge(_InputIter1 __first1, _InputIter1 __
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STL(标准模板库)提供了许多有用的算法函数,包括以下内容: 1. 非修改性序列操作: `std::all_of`, `std::any_of`, `std::none_of`, `std::for_each`, `std::count`, `std::count_if`, `std::mismatch`, `std::find`, `std::find_if`, `std::find_if_not`, `std::adjacent_find`, `std::search`, `std::search_n`, `std::equal`, `std::is_permutation`, `std::lexicographical_compare`. 2. 修改序列操作:`std::copy`, `std::copy_if`, `std::copy_n`, `std::copy_backward`, `std::move`, `std::move_backward`, `std::fill`, `std::fill_n`, `std::transform`, `std::generate`, `std::generate_n`, `std::replace`, `std::replace_if`, `std::replace_copy`, `std::replace_copy_if`, `std::swap`, `std::swap_ranges`, `std::iter_swap`, `std::reverse`, `std::reverse_copy`, `std::rotate`, `std::rotate_copy`, `std::unique`, `std::unique_copy`. 3. 排序和相关操作:`std::sort`, `std::stable_sort`, `std::partial_sort`, `std::partial_sort_copy`, `std::nth_element`. 4. 二分法操作:`std::lower_bound`, `std::upper_bound`, `std::binary_search`, `std::equal_range`. 5. 堆操作:`std::make_heap`, `std::push_heap`, `std::pop_heap`, `std::sort_heap`. 6. 集合操作:`std::merge`, `std::inplace_merge`, `std::includes`, `std::set_union`, `std::set_intersection`, `std::set_difference`, `std::set_symmetric_difference`. 7. 其他操作:`std::accumulate`, `std::iota`, `std::max`, `std::max_element`, `std::min`, `std::min_element`, `std::next_permutation`, `std::prev_permutation`. 这里只是列举了一些常见的算法函数,还有很多其他函数没有列举出来。这些函数可以在 `<algorithm>` 头文件中找到。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值