Transition from a vector with one dispenser to a vector with another

I have a vector<T, alloc> , where alloc is a simple wrapper on std::allocator<T> that does some additional account, like counting the number of objects created, etc.

Now I want to switch from vector<T, alloc> to vector<T> . None of the vector relocation functions seem to accept vector with different distributors.

How to move data from one vector to another, where the distributors do not match?

+6
source share
2 answers

As John Zwink explains in his answer, you cannot move a vector. However, you can move the elements of the vector as follows:

 vector<...> u; ... vector<...> v( std::make_move_iterator(u.begin()), std::make_move_iterator(u.end()) ); 

or

 vector<...> u; vector<...> v; ... v.insert( v.end(), std::make_move_iterator(u.begin()), std::make_move_iterator(u.end()) ); 

or (proposed by the Octalist)

 vector<...> u; vector<...> v; ... std::move(u.begin(), u.end(), std::back_inserter(v)); 
+3
source

This is really weird in STL. You will need insert() values โ€‹โ€‹of your source vector in your target vector. Or you can use one of several alternative universe STL implementations that directly affect this problem, for example:

Some of those who made the aforementioned quasi-STL implementations tried to get distributor model changes accepted in the C ++ standard, but they were not successful.

+3
source

Source: https://habr.com/ru/post/975954/


All Articles