1

How to copy last k items from std::multiset in reversed order to std::vector as fast as possible?

Johnas
  • 1,263
  • 3
  • 12
  • 23

1 Answers1

7

If you use the non-standard copy_n (you can easily roll your own), you can just do this:

std::copy_n(your_multiset.rbegin(), k, std::back_inserter(your_vector));

copy_n is part of C++1x, so there this solution is fully standard. If you want speed, it might be faster to reserve space in the vector in advance to save reallocations.

Community
  • 1
  • 1
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283