Generally I would strongly prefer v2 = v1:
- It is shorter and makes the intent more clear
std::copywon’t work ifv2doesn’t have the same length asv1(it won’t resize it, so it will retain some of the old elements best case (v2.size() > v1.size()and overwrite some random data used elsewhere in the program worst case- If
v1is about to expire (and you use C++11) you can easily modify it tomovethe contents - Performancewise assignment is unlikely to be slower then
std::copy, since the implementers would probably usestd::copyinternally, if it gave a performance benefit.
In conclusion, std::copy is less expressive, might do the wrong thing and isn’t even faster. So there isn’t really any reason to use it here.