Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) std::vector

 

STL container for storing instances of any data type.

 

Prefer using a std::vector over an array [1][2][3][4]. You can also create 2-dimensional matrices(or more dimensions).

 

Advantages of a std::vector over an array are:

  1. std::vector allocates memory from the free space when increasing in size
  2. std::vector is not a pointer in disguise [3]
  3. std::vector can increase/decrease in size run-time
  4. std::vector can do range checking (using at())

 

#include <iostream>
#include <vector>

int main()
{
  const std::size_t size = 100;
  std::vector<int> v(size);
  for (int i=0; i!=size; ++i)
  {
    v[i]=i; //Store in v
    std::cout << v[i] << std::endl; //Read from v
  }
}

 

 

 

 

 

The erase-remove idiom

 

Calling std::remove to remove a certain value from a std::vector does not change a std::vector its size. std::remove does return an iterator to where the removed elements are put. This iterator can be used to call std::vector its 'erase' member function. These two operations are called the erase-remove idiom.

 

Use the erase-remove idiom the really remove a value from a std::vector:

 

#include <cassert>
#include <algorithm>
#include <vector>

int main()
{
  const std::vector<int> v_expected = { 1,2,3,4,5,6,7 };
  std::vector<int> v = { 0,1,0,2,0,0,3,4,0,5,0,0,6,7,0 };

  //The erase-remove idiom
  v.erase(std::remove(v.begin(),v.end(),0),v.end());

  assert(v == v_expected);
}

 

 

 

 

 

 

std::vector code snippets

 

Note that among these are also more general container code snippets.

 

  1. AllAboutEqual, check if all doubles in a std::vector are about equal
  2. Append two std::vectors, Append
  3. Append, append two std::vectors
  4. Check if all doubles in a std::vector are about equal, AllAboutEqual
  5. CreateVector, create a std::vector from three values
  6. Convert Matrix<X> to Matrix<Y>, ConvertMatrix
  7. Convert std::vector<std::vector<X> > to std::vector<std::vector<Y> >, ConvertMatrix
  8. Convert two 2D std::vector<X> to 2D std::vector<Y>, ConvertMatrix
  9. ConvertMatrix, convert Matrix<X> to Matrix<Y>
  10. ConvertMatrix, convert std::vector<std::vector<X> > to std::vector<std::vector<Y> >
  11. ConvertMatrix, convert two 2D std::vector<X> to 2D std::vector<Y>
  12. CoutVector, std::cout on a std::vector
  13. GetIncVector, get a std::vector of incremented values (for example with values 0,1,2,3,etc)
  14. GetLongestString, find the length of the std::string with the most characters in a container
  15. GetShortestString, find the length of the std::string with the least characters in a container
  16. HugeVector, class that can contain more elements than std::vector
  17. LoopReader, reading a container looped
  18. RandomShuffle, shuffle a std::vector to a random order
  19. Read and write a std::vector from/to a std::stream
  20. Read and write two std::vectors from/to a std::stream
  21. Reading a container looped, LoopReader
  22. Save a container to file, SaveContainer
  23. SaveContainer, save a container to file
  24. SumStringLength, sum the lengths of std::strings irn a container
  25. Shuffle a std::vector to a random order, RandomShuffle
  26. Sort a std::vector, SortVector
  27. SortVector, sort a std::vector
  28. Write and read a std::vector to/from a std::stream
  29. Write and read two std::vectors to/from a std::stream
  30. std::cout on a std::vector, CoutVector

 

 

 

 

 

External links

 

 

 

 

 

 

References

 

  1. Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. 2005. ISBN: 0-32-111358-6. Chapter 76: 'Use vector by default. Otherwise, choose an appropriate container'.
  2. Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. 2005. ISBN: 0-32-111358-6. Chapter 77: 'Use vector and string instead of arrays'.
  3. Marshall Cline, Greg Lomow and Mike Girou. C++ FAQs. ISBN: 0-201-3098301. FAQ 28.02: 'Are arrays good or evil?' (Answer: 'Arrays are evil').
  4. Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Chapter C.14.11 'Prefer vector over array'.

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict