C++ Vector Output: Print Elements Easily

To output the elements in a vector, you can iterate through the vector and output each element. Here is an example code:

#include <iostream>
#include <vector>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};

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

    return 0;
}

In this example, we start by creating a vector containing integers, then we use a for loop to iterate through each element in the vector and print out the value using cout.

bannerAds