I have a std::vector<double> that I have to move to a boost::container::flat_set<double>.
Both containers are contiguous, so after sorting the vector in principle I could move the data from one to the other.
Is there a way to move the whole data between these two different containers?
Please, take into account that I want to move the whole data, not element by element.
I can move data between containers of the same type, but not between different containers.
std::vector<double> v1 = ...
std::sort(v1.begin(), v1.end());
std::vector<double> v2(std::move(v1)); // ok
boost::flat_set<double> f2(v1.begin(), v1.end()); // doesn't move, it copies
boost::flat_set<double> f3(std::move(v1)); // doesn't compile
It seems that for this to work flat_set should have a move constructor from containers with .data(), where the pointer is stolen from the argument.
I believe there is some way to verify whenever data alignment in both containers match and
memcpycould be used (and source cleared without destructing) exists and maybe someone will share it with us, but as long as we want to use STL there is a way: thestd::move_iterator. It makes your container constructor move elements instead of copying. It does not remove elements out of source container though, but leaves themstateless(e.g. empty strings as in example).Output
Online code: https://wandbox.org/permlink/ZLbocXKdqYHT0zYi