Lecture Notes for Advanced Programming II

5 February 2004 - STL Vectors


This code is verbose to get the idea across. If you're going to use this trick, you're probably going to use the more compact

  std::vector<int>().swap(iv)

instead. You can also use a variant of this trick to trim to fit:

std::vector<int> iv(10000, 0)
iv.resize(10)
std::vector<int>(iv).swap(iv)

This variant works only if the copy constructor sizes the new vector to fit the old one.


This page last modified on 9 February 2004.