Merge sorted containers
The std::merge()
algorithm takes two sorted sequences and creates a third merged and sorted sequence. This technique is often used as part of a merge sort, allowing very large amounts of data to be broken down into chunks, sorted separately, and merged into one sorted target.
How to do it…
For this recipe, we'll take two sorted vector
containers and merge them into a third vector
using std::merge()
.
- We'll start with a simple function to print the contents of a container:
void printc(const auto& c, string_view s = "") { if(s.size()) cout << format("{}: ", s); for(auto e : c) cout << format("{} ", e); cout << '\n'; }
We'll use this to print the source and destination sequences.
- In the
main()
function, we'll declare our source vectors, along with the destination vector, and print...