In this C++ tutorial, you will learn how to print the elements of a vector using a range-based for loop, while loop, indexed for loop, iterators, and reverse iterators. You will also see how to print vector elements on one line and how to print a two-dimensional vector.

Print Vector Elements in C++

A C++ vector does not have a built-in member function that directly prints all of its elements. To display the contents of a vector, iterate through its elements and send each value to std::cout.

For most cases where you only need the values, a range-based for loop is the simplest approach. An indexed loop is useful when you also need each element’s position, while iterators are useful when working with iterator-based C++ code.

1. Print vector elements using ForEach

In the following program, we shall use C++ Foreach statement to print the elements of vector.

C++ Program

</>
Copy
#include <iostream>
#include <vector>
using namespace std;

int main() {
   vector<int> nums;
   nums.push_back(53);
   nums.push_back(47);
   nums.push_back(85);
   nums.push_back(92);

   for (int element: nums)
      cout << element << endl;
}

Output

53
47
85
92

The range-based for loop visits each element from the beginning of the vector to the end. Since only the values are required here, there is no need to manage an index manually.

2. Print vector elements using While loop

In the following program, we shall use C++ While Loop to print the elements of vector from starting to end. We shall use index to access an element of vector during each iteration.

C++ Program

</>
Copy
#include <iostream>
#include <vector>
using namespace std;

int main() {
   vector<int> nums;
   nums.push_back(53);
   nums.push_back(47);
   nums.push_back(85);
   nums.push_back(92);

   int i = 0;
   while (i < nums.size()) {
      cout << nums[i] << endl;
      i++;
   }     
}

Output

53
47
85
92

The loop starts at index 0 and continues while the index is less than nums.size(). The expression nums[i] accesses the vector element at the current index.

3. Print vector elements using For loop

In the following program, we shall use C++ For Loop to print the elements of vector. As in while loop, we shall use index to access elements of vector in this example as well.

C++ Program

</>
Copy
#include <iostream>
#include <vector>
using namespace std;

int main() {
   vector<int> nums;
   nums.push_back(53);
   nums.push_back(47);
   nums.push_back(85);
   nums.push_back(92);

   for (int i = 0; i < nums.size(); i++) {
      cout << nums[i] << endl;
   }     
}

Output

53
47
85
92

An indexed for loop is convenient when the position of each value matters. For example, you can print both the index and the element during the same iteration.

4. Print C++ vector elements using an iterator

A vector provides begin() and end() iterators. begin() refers to the first element, while end() refers to the position immediately after the last element. Increment the iterator until it reaches end(), and dereference it with * to obtain the current value.

</>
Copy
#include <iostream>
#include <vector>

int main() {
    std::vector<int> nums = {53, 47, 85, 92};

    for (auto it = nums.begin(); it != nums.end(); ++it) {
        std::cout << *it << '\n';
    }
}

Output

53
47
85
92

Iterator-based traversal is particularly useful when the surrounding code already works with iterators or iterator ranges.

5. Print vector elements on one line in C++

To print a vector on one line, output a separator such as a space instead of endl after every element.

</>
Copy
#include <iostream>
#include <vector>

int main() {
    std::vector<int> nums = {53, 47, 85, 92};

    for (int value : nums) {
        std::cout << value << ' ';
    }

    std::cout << '\n';
}

Output

53 47 85 92

You can replace the space with another separator, such as a comma. If formatting must not contain a trailing separator after the final element, use an index or iterator to identify the last element.

6. Print vector elements with indexes in C++

When you need both the position and value, use an indexed loop. A vector’s valid indexes run from 0 through size() - 1 when the vector is not empty.

</>
Copy
#include <iostream>
#include <vector>

int main() {
    std::vector<int> nums = {53, 47, 85, 92};

    for (std::size_t i = 0; i < nums.size(); ++i) {
        std::cout << "nums[" << i << "] = " << nums[i] << '\n';
    }
}

Output

nums[0] = 53
nums[1] = 47
nums[2] = 85
nums[3] = 92

std::size_t is appropriate for the index because vector::size() returns an unsigned size type.

7. Print C++ vector elements in reverse order

Use the vector’s reverse iterators when the elements need to be printed from the last element to the first. rbegin() starts at the last element and rend() marks the position before the first element in reverse traversal.

</>
Copy
#include <iostream>
#include <vector>

int main() {
    std::vector<int> nums = {53, 47, 85, 92};

    for (auto it = nums.rbegin(); it != nums.rend(); ++it) {
        std::cout << *it << '\n';
    }
}

Output

92
85
47
53

This only changes the order in which the values are visited. It does not modify the order stored in the vector.

Print a 2D Vector in C++

A two-dimensional vector is a vector whose elements are themselves vectors. Use a nested loop: the outer loop visits each row, and the inner loop prints the elements in that row.

</>
Copy
#include <iostream>
#include <vector>

int main() {
    std::vector<std::vector<int>> matrix = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    for (const auto& row : matrix) {
        for (int value : row) {
            std::cout << value << ' ';
        }
        std::cout << '\n';
    }
}

Output

1 2 3
4 5 6
7 8 9

The outer loop uses const auto& so that each row is accessed by reference rather than copied.

Choosing a Loop to Print C++ Vector Elements

  • Range-based for loop: use it when you only need each vector value.
  • Indexed for loop: use it when you need both the element and its index.
  • While loop: use it when loop progression is controlled separately from initialization.
  • Iterator loop: use it when working with iterator-based algorithms or ranges.
  • Reverse iterator loop: use it when values must be displayed from the end of the vector to the beginning.
  • Nested loops: use them to print rows and columns of a 2D vector.

All of these approaches visit the vector elements without requiring the vector to be resized or otherwise modified. For simple sequential output, a range-based for loop is usually the most concise option.

Important Details When Printing std::vector Values

  • vector::size() gives the current number of elements in the vector.
  • With indexed access, keep the index strictly less than size().
  • A range-based for loop automatically stops after the last element.
  • Printing an empty vector with any of these loops produces no element output because the loop body does not execute.
  • If the elements are expensive objects and you only need to read them, a range-based loop can use const auto& to avoid copying each element.

In this C++ Tutorial, we learned how to print vector elements using range-based, while, indexed for, iterator, and reverse-iterator loops. We also covered printing vector values on one line, displaying their indexes, and printing a two-dimensional vector.