Skip to content

Instantly share code, notes, and snippets.

@addam
Last active May 25, 2016 10:07
Show Gist options
  • Save addam/8e23efd832643155870230c2d9d90cf7 to your computer and use it in GitHub Desktop.
Save addam/8e23efd832643155870230c2d9d90cf7 to your computer and use it in GitHub Desktop.
function to copy-merge one sorted vector into another, in place
template<typename T>
void inmerge(std::vector<T> &target, const std::vector<T> &source)
{
target.resize(target.size() + source.size());
auto lit = target.crbegin() + source.size(), rit = source.rbegin();
auto lend = target.crend(), rend = source.rend();
auto out = target.rbegin();
while (rit != rend) {
*(out++) = (lit != lend and *rit < *lit) ? *(lit++) : *(rit++);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment