C++ Vectors

Vectors in C++ are dynamic arrays that can resize themselves automatically when elements are added or removed. They are part of the Standard Template Library (STL) and provide a convenient way to manage collections of data.

Key Topics

Overview of Vectors

Vectors are implemented as a template class in the vector header. They provide several advantages:

  • Dynamic sizing: Vectors can grow and shrink as needed.
  • Random access: Elements can be accessed directly using an index.
  • Memory management: Vectors handle memory allocation and deallocation automatically.

Declaring Vectors

To declare a vector, include the vector header and use the following syntax:

#include 
vector myVector;

Common Vector Operations

Here are some common operations you can perform on vectors:

  • push_back(): Adds an element to the end of the vector.
  • pop_back(): Removes the last element from the vector.
  • size(): Returns the number of elements in the vector.
  • clear(): Removes all elements from the vector.

Example: Basic Vector Operations

#include 
#include 
using namespace std;

int main() {
    vector myVector;
    myVector.push_back(10);
    myVector.push_back(20);
    myVector.push_back(30);
    cout << "Vector size: " << myVector.size() << endl;
    myVector.pop_back();
    cout << "Vector size after pop: " << myVector.size() << endl;
    myVector.clear();
    cout << "Vector size after clear: " << myVector.size() << endl;
    return 0;
}

Output:

Vector size: 3 Vector size after pop: 2 Vector size after clear: 0

Iterating Over Vectors

You can iterate over the elements of a vector using a range-based for loop or traditional for loop.

Example: Iterating with a Range-Based For Loop

#include 
#include 
using namespace std;

int main() {
    vector myVector = {10, 20, 30, 40, 50};
    cout << "Vector elements: ";
    for (int num : myVector) {
        cout << num << " ";
    }
    return 0;
}

Output:

Vector elements: 10 20 30 40 50

Example: Iterating with a Traditional For Loop

#include 
#include 
using namespace std;

int main() {
    vector myVector = {10, 20, 30, 40, 50};
    cout << "Vector elements using traditional for loop: ";
    for (size_t i = 0; i < myVector.size(); ++i) {
        cout << myVector[i] << " ";
    }
    return 0;
}

Output:

Vector elements using traditional for loop: 10 20 30 40 50

Exercises with Vectors

Here are some exercises to practice your understanding of vectors:

  • Create a vector of strings and store names. Print each name using a range-based for loop.
  • Write a program that reads integers from the user and stores them in a vector until the user enters -1. Then, print the sum of the entered integers.
  • Implement a function that takes a vector and returns the maximum element.

Key Takeaways

  • Vectors are dynamic arrays that can resize automatically, making them versatile for managing collections of data.
  • Common operations include adding, removing, and accessing elements, as well as checking the size of the vector.
  • Vectors support iteration using both range-based for loops and traditional for loops.
  • Practice exercises can help solidify your understanding of how to use vectors effectively in C++.