std::merge
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| Defined in header <algorithm>
|
||
| template< class InputIt1, class InputIt2, class OutputIt > OutputIt merge( InputIt1 first1, InputIt1 last1, |
(1) | |
| template< class InputIt1, class InputIt2, class OutputIt, class Compare> OutputIt merge( InputIt1 first1, InputIt1 last1, |
(2) | |
[first1, last1),[first2, last2)成一个有序区间开始在d_first。 operator<的第一个版本使用比较的元素,第二个版本使用给定的比较函数comp。等效的元件的相对顺序被保留.[first1, last1) and [first2, last2) into one sorted range beginning at d_first. The first version uses operator< to compare the elements, the second version uses the given comparison function comp. The relative order of equivalent elements is preserved.You can help to correct and verify the translation. Click here for instructions.
目录 |
[编辑] 参数
| first1, last1 | - | 第一个范围的元素进行合并
Original: the first range of elements to merge The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |||||||||
| first2, last2 | - | 要合并的元素的第二范围
Original: the second range of elements to merge The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |||||||||
| d_first | - | 的目标范围的开头
Original: the beginning of the destination range The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |||||||||
| comp | - | comparison function which returns true if the first argument is less than the second. The signature of the comparison function should be equivalent to the following:
The signature does not need to have const &, but the function must not modify the objects passed to it. | |||||||||
| Type requirements | |||||||||||
-InputIt1 must meet the requirements of InputIterator.
| |||||||||||
-InputIt2 must meet the requirements of InputIterator.
| |||||||||||
-OutputIt must meet the requirements of OutputIterator.
| |||||||||||
[编辑] 返回值
You can help to correct and verify the translation. Click here for instructions.
[编辑] 复杂性
You can help to correct and verify the translation. Click here for instructions.
[编辑] 可能的实现
| First version |
|---|
template<class InputIt1, class InputIt2, class OutputIt> OutputIt merge(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first) { for (; first1 != last1; ++d_first) { if (first2 == last2) { return std::copy(first1, last1, d_first); } if (*first2 < *first1) { *d_first = *first2; ++first2; } else { *d_first = *first1; ++first1; } } return std::copy(first2, last2, d_first); } |
| Second version |
template<class InputIt1, class InputIt2, class OutputIt, class Compare> OutputIt merge(InputIt1 first1, InputIt1 last1, 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); } |
[编辑] 为例
#include <iostream> #include <iterator> #include <cstdlib> #include <algorithm> #include <vector> int main() { const std::size_t items = 10; std::vector<int> v1, v2, dst; // fill the vectors for (std::size_t idx = 0; idx < items; ++idx) { v1.push_back(std::rand()%items); v2.push_back(std::rand()%items); } // sort std::sort(v1.begin(), v1.end()); std::sort(v2.begin(), v2.end()); // output v1 std::cout << "v1 : "; std::copy(v1.begin(), v1.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; // output v2 std::cout << "v2 : "; std::copy(v2.begin(), v2.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; // merge std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(dst)); // output std::cout << "dst: "; std::copy(dst.begin(), dst.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; }
Output:
v1 : 0 0 2 2 3 3 3 6 7 9 v2 : 1 2 5 5 6 6 6 6 7 9 dst: 0 0 1 2 2 2 3 3 3 5 5 6 6 6 6 6 7 7 9 9
[编辑] 另请参阅
| 合并两个有序的范围内的地方 Original: merges two ordered ranges in-place The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) | |
| 为升序排序范围 Original: sorts a range into ascending order The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) | |
| 排序范围内的元素,同时保持相等的元素之间的顺序 Original: sorts a range of elements while preserving order between equal elements The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) | |