Working with Vectors in C++

This code snippet introduces the essential concept of using std::vector in C++ to manage dynamic arrays, offering a simple example of storing and displaying a list of integers.

Working with Vectors in C++
Photo by Philipp Katzenberger / Unsplash

This code snippet introduces the essential concept of using std::vector in C++ to manage dynamic arrays, offering a simple example of storing and displaying a list of integers.

#include <iostream>
#include <vector>

int main() {
    std::vector<int> numbers;
    numbers.push_back(1);
    numbers.push_back(2);
    numbers.push_back(3);

    std::cout << "Vector elements:";
    for (int i = 0; i < numbers.size(); ++i) {
        std::cout << " " << numbers[i];
    }
    std::cout << std::endl;

    return 0;
}