Move STD:: vector to STD:: deque in C 11

If I have STD:: deque and STD:: vector and want to combine them into STD:: deque, I can do it in the following ways:

typedef int T; // type int will serve just for illustration
std::deque< T > deq(100); // just some random size here
std::vector< T > vec(50);
// ... doing some filling ...
// Now moving vector to the end of queue:
deq.insert( 
    deq.end(),std::make_move_iterator( vec.begin() ),std::make_move_iterator( vec.end() )
);
std::cout << deq.size() << std::endl;

We know the size of the vector, but we are using STD:: deque We cannot reserve memory at the end of STD:: deque until insert (...) So is it the fastest way to move all the elements of STD:: vector to the end of STD:: deque? Or did I miss something?

thank you.

Solution

I will use the following resizing method, because only deque is reassigned once:

size_t oldSize = deq.size();
deq.resize(deq.size() + vec.size());
copy(vec.begin(),vec.end(),deq.begin() + oldSize);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>