. Advertisement .
..3..
. Advertisement .
..4..
Vectors are one of the main sequence containers in the C++ Standard Template Library (STL). They provide many operations absent from the Standard Library. Check this guide out if you need to print a vector in C++.
How To Print A Vector In C++
Using for Loops With Indices
The most obvious solution for most of us is to iterate through the vector and print its element one by one. C++ allows you to index a vector like an array, meaning this task is also similar to printing all the elements of a C++ array.
Example:
#include <vector>
#include <iostream>
using namespace std;
void indices(vector<int> data) {
int length = data.size();
for (int i = 0; i < length; i++)
{
cout << data[i] << "\n";
}
}
int main() {
vector<int> data{10, 20, 30, 40, 50, 60};
indices(data);
return 0;
}
Output:
10
20
30
40
50
60
Using for Loops With vector::begin() And vector::end()
Besides using a regular iterator, you can also rely on vector::begin() and vector::end(). These functions return bidirectional iterators that point to the first and next to the last elements of a vector. They are available in the <vector> header.
Example:
#include <vector>
#include <iostream>
using namespace std;
void iterators(vector<int> data) {
for(auto itr=data.begin(); itr!=data.end(); itr++)
{
cout<<*itr<<"\n";
}
}
int main() {
vector<int> data{10, 20, 30, 40, 50, 60};
iterators(data);
return 0;
}
Output:
10
20
30
40
50
60
Using Range-Based for Loops
The C++11 standard introduced range-based loops, which go over a predefined range. Vectors support this new type of looping.
Syntax:
for (declaration : expression) {
statement
}
The expression parameter represents a sequence or a suitable braced-init-list in C++. The declaration in a range-based loop is a variable that has the type of the elements of expression.
Example:
#include <vector>
#include <iostream>
using namespace std;
void range_based(vector<int> data) {
for (int x : data) {
cout << x << "\n";
}
}
int main() {
vector<int> data{10, 20, 30, 40, 50, 60};
range_based(data);
return 0;
}
Output:
10
20
30
40
50
60
Using for_each Loops
In addition to for, do-while, and while, C++ has for_each that you can use to get the job done. It executes a function over each element of a container. You must include the <algorithm> header file to use for_each.
Example:
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void print_vector(int element)
{
cout << element << "\n";
}
void foreach(vector<int> data) {
for_each(data.begin(), data.end(), print_vector);
}
int main() {
vector<int> data{10, 20, 30, 40, 50, 60};
foreach(data);
return 0;
}
Output:
10
20
30
40
50
60
Using copy()
C++ STL, in particular the header <algorithm>, comes with the copy() function that enables several copy operations.
It accepts three pointers (the beginning and the end of the source container) and the beginning of the destination container. You can declare an output iterator object as the destination container in order to print a vector in C++.
Example:
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
void copyvector(vector<int> data) {
ostream_iterator<int> out_itr (cout, "\n");
copy(data.begin(), data.end(), out_itr);
}
int main() {
vector<int> data{10, 20, 30, 40, 50, 60};
copyvector(data);
return 0;
}
Output:
10
20
30
40
50
60
Conclusion
There are multiple ways to print a vector in C++. Most of them depend heavily on for loops, even though they use different kinds of iterators provided by this programming language.
Leave a comment