I have a vector of chars which contains some data elements.
std::vector<unsigned char> data_1;
I have an unsigned char * which is pointing another set of data elements.
unsigned char * data_2;
Question:
Is there a way I can merge data_2
into data_1
which is a vector without causing a copy of the data elements at all?
I read about move semantics being referred in this discussion but I am a bit unsure if it is possible in this situation I have here.
Is there a way I can merge data_2 into data_1 which is a vector without causing a copy of the data elements at all?
No. All ways of inserting elements into a vector require copying (or moving) each element at least once.
I read about move semantics being referred in this discussion but I am a bit unsure if it is possible in this situation I have here.
Moving a char is same as copying a char. The distinction is only relevant to class types with a non-trivial move constructor or move assignment operator.
It wouldn't be possible even if data_2 was a
std::vector<unsigned char>
?
Appending without copying (or moving) would not be possible even then.
Sidenote 1: You can replace the entire content of one vector with content of another vector without copying (or moving) any elements by using move assignment operator of the vector.
Sidenote 2: You can merge two instances of node based containers such as std::list
s, std::set
s, std::map
s and their unordered counterparts without copying (nor moving) any of the elements.