一 概述
C++ 标准库中提供了很多算法,定义于头文件 < algorithm >。本文主要探究以下用于 归并排序区间元素 的算法:
std::merge 归并二个已排序范围 [first1, last1) 和 [first2, last2) 到另一个已排序范围中
二 辅助函数
三 定义
template < class InputIt1 , class InputIt2 , class OutputIt >
OutputIt merge ( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first ) ; ( 1 ) ( C++ 20 前)
template < class InputIt1 , class InputIt2 , class OutputIt >
constexpr OutputIt merge ( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first ) ; ( 1 ) ( C++ 20 起)
template < class ExecutionPolicy , class ForwardIt1 , class ForwardIt2 , class ForwardIt3 >
ForwardIt3 merge ( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2,
ForwardIt3 d_first ) ; ( 2 ) ( C++ 17 起)
template < class InputIt1 , class InputIt2 , class OutputIt , class Compare >
OutputIt merge ( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first, Compare comp ) ; ( 3 ) ( C++ 20 前)
template < class InputIt1 , class InputIt2 , class OutputIt , class Compare >
constexpr OutputIt merge ( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first, Compare comp ) ; ( 3 ) ( C++ 20 起)
template < class ExecutionPolicy , class ForwardIt1 , class ForwardIt2 , class ForwardIt3 , class Compare >
ForwardIt3 merge ( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2,
ForwardIt3 d_first, Compare comp ) ; ( 4 ) ( C++ 17 起)
ExecutionPolicy 请参考此前文章 std::find std::execution。 若对于任何指向序列的迭代器 it 与任何使得 it + n 为指向序列元素的合法迭代器的非负整数 n , comp(*(it + n), *it) 求值为 false ,则称序列相对于 comp 已排序。
四 Demo
if ( 1 ) {
std:: list< int > l;
std:: vector< int > vc1;
fill ( vc1, 1 , 9 ) ;
std:: vector< int > vc2;
fill ( vc2, 1 , 9 ) ;
print ( "before merge vc1(src): " , vc1) ;
print ( "before merge vc2(src): " , vc2) ;
print ( "before merge list(dst): " , l) ;
std:: merge ( vc1. begin ( ) , vc1. end ( ) , vc2. begin ( ) , vc2. end ( ) ,
std:: back_inserter ( l) ) ;
print ( "after merge vc1(src): " , vc1) ;
print ( "after merge vc2(src): " , vc2) ;
print ( "after merge list(dst): " , l) ;
}
if ( 1 ) {
std:: list< int > l;
std:: vector< int > vc1;
fill ( vc1, 1 , 9 ) ;
std:: vector< int > vc2;
fill ( vc2, 1 , 9 ) ;
print ( "before merge vc1(src): " , vc1) ;
print ( "before merge vc2(src): " , vc2) ;
print ( "before merge list(dst): " , l) ;
std:: merge ( vc1. begin ( ) , vc1. end ( ) , vc2. begin ( ) , vc2. end ( ) ,
std:: back_inserter ( l) , std:: less< int > ( ) ) ;
print ( "after merge vc1(src): " , vc1) ;
print ( "after merge vc2(src): " , vc2) ;
print ( "after merge list(dst): " , l) ;
}
before merge vc1 ( src) : 1 2 3 4 5 6 7 8 9
before merge vc2 ( src) : 1 2 3 4 5 6 7 8 9
before merge list ( dst) :
after merge vc1 ( src) : 1 2 3 4 5 6 7 8 9
after merge vc2 ( src) : 1 2 3 4 5 6 7 8 9
after merge list ( dst) : 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
before merge vc1 ( src) : 1 2 3 4 5 6 7 8 9
before merge vc2 ( src) : 1 2 3 4 5 6 7 8 9
before merge list ( dst) :
after merge vc1 ( src) : 1 2 3 4 5 6 7 8 9
after merge vc2 ( src) : 1 2 3 4 5 6 7 8 9
after merge list ( dst) : 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
五 参考