Say I have a std::vector called vec with 10 elements and I want to create a new std::vector containing all elements between (and including) the 2nd and 5th elements of vec. I can see how I might write a for loop to do this, but it looks like STL’s copy() can do this more concisely. But I’m not really getting iterators: I’ve seen how you can use start() and end() to iterate over a vector from its first to last element, but what about the situation above, where I want something slightly different? Thanks.
Say I have a std::vector called vec with 10 elements and I want to
Share
You don’t need
std::copyto create a newvectorwith a subset of the first one. You can achieve this with thevector‘s constructor and its iterators (doc here):You have to be sure though, that you don’t exceed the
vector‘s limits, or you will get undefined behavior.