一:功能
将一个无序序列分成两部分,然后将其合并成有序序列。
二:用法
#include <vector>
#include <algorithm>
#include <iostream>int main() {std::vector<int> range{1, 3, 5, 2, 4, 6};std::inplace_merge(range.begin(), range.begin()+3, range.end());for (auto v : range) {std::cout << v << " ";}std::cout << "\n";
}
#include <vector>
#include <iostream>
#include <algorithm>template<class Iter>
void merge_sort(Iter first, Iter last)
{if (last - first > 1) {Iter middle = first + (last - first) / 2;merge_sort(first, middle);merge_sort(middle, last);std::inplace_merge(first, middle, last);}
}int main()
{std::vector<int> v{8, 2, -2, 0, 11, 11, 1, 7, 3};merge_sort(v.begin(), v.end());for(auto n : v) {std::cout << n << ' ';}std::cout << '\n';
}