I have an std::vector, and I want a separate std::vector containing only the last n elements of the original vector. Besides a loop over the entire vector inserting one-by-one, is there a cleaner way of doing this?
I have an std::vector , and I want a separate std::vector containing only the
Share
Of course, this will crash and burn if
x.size()<nTo elaborate a little,
std::vector(like most of the standard library containers) has a constructor which takes a pair of iterators. It fills the vector with all the items from the first iterator up to the second.