Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何尽快将 std::multiset 中的最后 k 个项目以相反的顺序复制到 std::vector?
如果您使用非标准copy_n(您可以轻松滚动自己的),您可以这样做:
copy_n
std::copy_n(your_multiset.rbegin(), k, std::back_inserter(your_vector));
copy_n是 C++1x 的一部分,所以这个解决方案是完全标准的。如果你想要速度,提前在向量中预留空间以节省重新分配可能会更快。