How to Print a Vector in C++
You can’t just write std::cout << myVector — C++ has no built-in way to print a whole vector at once. Instead you loop over the elements and print them yourself. Here are four clean ways to do it, from the beginner favorite to a reusable helper.
Method 1: Range-Based For Loop (Recommended)
This is the modern, readable default. It walks every element without you managing indices:
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {10, 20, 30, 40};
for (int n : numbers)
std::cout << n << " ";
std::cout << "\n"; // 10 20 30 40
return 0;
}
for (int n : numbers) reads as “for each n in numbers.” It’s the cleanest option for simply showing every value, and it works for any type the stream understands.
Method 2: Index Loop (When You Need Positions)
If you want to print the index alongside each value, a classic counting loop gives you that control:
#include <iostream>
#include <vector>
#include <string>
int main() {
std::vector<std::string> names = {"Ann", "Bob", "Cy"};
for (std::size_t i = 0; i < names.size(); ++i)
std::cout << i << ": " << names[i] << "\n";
return 0;
}
Using std::size_t for i matches the type that size() returns, which avoids a common compiler warning. Reach for this when the position matters, not just the value.
Method 3: Iterators
Iterators are the older, more explicit way to walk a container. You rarely need them just to print, but it’s worth recognizing the pattern:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3};
for (auto it = v.begin(); it != v.end(); ++it)
std::cout << *it << " ";
std::cout << "\n";
return 0;
}
v.begin() points to the first element and v.end() points just past the last. The *it reads the value the iterator currently points to. The range-based loop in Method 1 is really this pattern with the boilerplate hidden.
Method 4: A Reusable Print Function
If you print vectors a lot, write the loop once as a template so it works for int, double, std::string, and more:
#include <iostream>
#include <vector>
template <typename T>
void printVector(const std::vector<T>& v) {
std::cout << "[ ";
for (const T& item : v)
std::cout << item << " ";
std::cout << "]\n";
}
int main() {
printVector(std::vector<int>{1, 2, 3});
printVector(std::vector<double>{1.5, 2.5});
return 0;
}
Passing the vector by const reference avoids copying it, and the template means one function handles every element type. This is the version worth keeping in your toolbox.
Bonus: Printing a 2D Vector
A vector of vectors prints with two nested loops — one for rows, one for the values in each row:
#include <iostream>
#include <vector>
int main() {
std::vector<std::vector<int>> grid = {{1, 2}, {3, 4}};
for (const auto& row : grid) {
for (int cell : row)
std::cout << cell << " ";
std::cout << "\n";
}
return 0;
}
The outer loop hands you one row at a time; the inner loop prints it. The newline after the inner loop keeps the grid lined up neatly.
Related Articles
- C++ Vector Tutorial — the complete guide to std::vector
- C++ Range-Based For Loop — the modern way to loop
- C++ Find an Element in a Vector — search a vector with std::find
- C++ 2D Vector — grids and matrices that resize
- C++ Iterators — how begin() and end() really work
Take Your C++ Further
If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.