Lecture Notes for Advanced Programming II

5 February 2004 - STL Vectors


Outline


Vector Behavior


Vector Definition


Vector Constructors


Vector Element Access


.at() vs. []


Growing and Shrinking Vectors


Vector Storage Management


Vector Growth


Vector Storage Management Functions


Vector Storage Query Functions


Vector Storage Management Functions


And the Winner Is


Vector Shrinking, Revisited

std::vector<int> iv(10000, 0)
print(iv)
iv.clear()
print(iv)

iv.capacity() = 10000, iv.size() = 10000
iv.capacity() = 10000, iv.size() = 0

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

iv.capacity() = 10000, iv.size() = 10000
iv.capacity() = 10000, iv.size() = 0


Shrinking via Swapping

std::vector<int> iv(10000, 0)
print(iv)

{ std::vector<int> lamb
  print(lamb)
  lamb.swap(iv)    // or iv.swap(lamb)
  print(lamb)
  }

print(iv)

iv.capacity() = 10000, iv.size() = 10000

lamb.capacity() = 0, lamb.size() = 0

lamb.capacity() = 10000, lamb.size() = 10000

iv.capacity() = 0, iv.size() = 0


Vector Relation Operators


Vector Less-Than


Lexicographic Order


Vector Type Definitions


Vector Miscellany


Points to Remember


This page last modified on 9 February 2004.