Consider the following snippet (saw something analogous to this in a large simulation code)
std::vector<int> v1{1,2,3,4,5,6,7};
std::vector<int> v2;
std::move(v1.begin() + 2, v1.end(), back_inserter(v2));
Here, I am moving a range of elements from v1
to v2
, but is there any particular advantage to doing this vs. copying? I do not actually see what the advantage of the move
here would be since it's operating on a range of int
s. In fact, I do not believe any move is occurring since we are dealing with POD types.
If we instead wanted to transfer the entire v1
to v2
, then we could do:
v2 = std::move(v1);
The cast here would allow v2
to now own the pointer to the contiguous range of memory previously owned by v1
, thus avoiding a copy.
But in the former move of a range of elements, I do not see the usefulness.
Aucun commentaire:
Enregistrer un commentaire