The vector class

Objectives

  • Demonstrate the use of the vector class
  • Demonstrate techniques with vectors

vector class

  • Vectors are like having a dynamic array that you can add and remove items from
  • Use: #include <vector>
  • Use: using std::vector;
  • Example using an imaginary Pet class:
    • Declare a vector to hold Pet objects: vector<Pet> pets;
    • Create a Pet object: Pet pet(param1, param2, ...);
    • Add the Pet object to the vector: pets.push_back(pet);
    • Accessing the first Pet object in the vector: pets[0]
    • Getting the size/length of the vector: pets.size()
    • Accessing the last Pet object in the vector: pets[pets.size()-1]