HOWTO · C++
How to Print Out the Contents of a Vector in C++
This article introduces how to print vector contents to stdout in C++. Learn various methods such as using loops, the std::copy function, and custom functions to display data effectively. Enhance your C++ programming skills with these clear examples and detailed explanations. Perfect for beginners and experienced developers alike.
On this page
Printing the contents of a vector in C++
Printing every element of a std::vector means iterating over its elements and writing each one to std::cout. You can use an indexed loop, a range-based loop, std::copy, or std::for_each. The examples use C++11 or later, because range-based loops, auto, lambdas, and initializer-list syntax require that standard level.
Print vector elements with an indexed loop
An indexed loop is useful when you need the element position as well as its value. Use at() when you want bounds checking; it reports an out-of-range access. operator[] is concise, but the index must already be valid and the operator does not check it for you.
Run this example:
#include <iostream>
#include <cstddef>
#include <vector>
int main() {
const std::vector<int> values{10, 20, 30};
for (std::size_t i = 0; i < values.size(); ++i) {
if (i != 0) std::cout << ' ';
std::cout << values.at(i);
}
std::cout << '\n';
for (std::size_t i = 0; i < values.size(); ++i) {
if (i != 0) std::cout << ' ';
std::cout << values[i];
}
std::cout << '\n';
}
10 20 30
10 20 30
The loop condition keeps each index below values.size(). For an empty vector, the body runs zero times, so only the explicit newline is written.
Print vector elements with a range-based for loop
A range-based loop visits each element without maintaining an index. Bind each value as const auto& to avoid copying it, which is especially useful when elements are larger than integers.
#include <iostream>
#include <string>
#include <vector>
int main() {
const std::vector<std::string> names{"Ada", "Grace", "Linus"};
bool first = true;
for (const auto& name : names) {
if (!first) std::cout << ' ';
std::cout << name;
first = false;
}
std::cout << '\n';
}
Ada Grace Linus
The loop writes spaces only between values, so there is no trailing separator. An empty vector prints no values; the newline is still printed.
Print vector elements with std::copy
std::copy transfers an iterator range to an output iterator. Include <algorithm> for the algorithm and <iterator> for std::ostream_iterator. Its second iterator marks the end of the range and is not dereferenced.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main() {
const std::vector<int> values{4, 8, 12};
std::copy(values.begin(), values.end(),
std::ostream_iterator<int>(std::cout, ","));
std::cout << '\n';
}
4,8,12,
The delimiter passed to std::ostream_iterator is written after every element, including the last, so this example leaves a comma before the newline. The empty range writes nothing. Choose a different delimiter or a separate separator strategy if a trailing delimiter is undesirable. This method is concise when the operation is simply to send a range to a stream.
Print vector elements with std::for_each
std::for_each calls the supplied function once for each element in the iterator range. A lambda makes the output action visible at the call site.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
const std::vector<int> values{2, 4, 6};
std::for_each(values.begin(), values.end(), [](int value) {
std::cout << value << ';';
});
std::cout << '\n';
}
2;4;6;
Here the lambda writes a semicolon after each value, including the final one. It is also possible to capture a separator state when you need punctuation only between elements. An empty range invokes the lambda zero times.
Reuse a function to print vectors
A function is useful when several parts of a program need the same formatting. This template accepts vectors of different element types, provided each element can be inserted into an output stream with operator<<.
#include <iostream>
#include <vector>
template <typename T>
void printVector(const std::vector<T>& values) {
bool first = true;
for (const auto& value : values) {
if (!first) {
std::cout << ' ';
}
std::cout << value;
first = false;
}
std::cout << '\n';
}
int main() {
const std::vector<char> letters{'C', '+', '+'};
printVector(letters);
}
C + +
This helper places spaces only between elements and writes a newline even when the vector has no elements. For a custom type, define a suitable stream insertion overload before calling the helper.
Choose a vector-printing approach
Use an indexed loop when the position matters or when you need at() bounds checks. A range-based loop is often the most direct choice when you only need to visit each value. Use std::copy for straightforward range-to-stream output, keeping its trailing delimiter behavior in mind. std::for_each fits when each element needs an operation expressed as a callable. Put formatting in a reusable function when multiple call sites should share it. These choices are about clarity, bounds behavior, and reuse; there is no evidence here that one is universally faster.