Different Methods to Print Elements of Vector in C++
Last Updated :
29 Nov, 2024
In this article, we will learn different methods to print the elements of vector in C++.
The easiest method to print the elements of vector is by using range based for loop. Let’s take a look at an example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5};
// Printing elements of vector
for (auto i : v)
cout << i << " ";
return 0;
}
The range-based loop does not need any iterator or size information. It just needs the name of vector.
There are also some other methods in C++ to print the elements of vector. Some of them are as follows:
Using Traditional Loop
Iterate the vector and print each element of the vector using its index till the index is less than the size. The vector size() method can be used to find the size of the vector.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5};
// Print vector elements
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
return 0;
}
This method provides more control over the printing process.
Using Loop with Iterators
The above method can be modified to work with iterators instead of the index value and size. The vector begin() and vector end() methods to find the iterators to the beginning and the end of the vector. Then a loop can be used to print all elements one by one by incrementing the beginning iterator till it is less than end.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = { 1, 2, 3, 4, 5 };
// Print vector elements
for (auto i = v.begin(); i != v.end(); ++i)
cout << *i << " ";
return 0;
}
This method is preferred when you need to work with STL algorithms.
Using for_each()
The for_each() algorithm can be used to apply the given operation to each element in the vector. Apply the print operation to each element of the vector by passing the function to print a single element.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = { 1, 2, 3, 4, 5 };
// Print vector elements
for_each(v.begin(), v.end(), [](int i) {
cout << i << " ";
});
return 0;
}
The for_each_n() algorithm is similar to for_each but it allows specifying the number of elements to process. This method is useful for processing a subset of the vector.
Using copy() and ostream_iterator
The ostream_iterator allows the use of output stream as an iterable object and use it in the STL algorithms. Copy the vector elements to ostream_iterator to cout stream using copy() method to print them to the output stream.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = { 1, 2, 3, 4, 5 };
// Print vector elements
copy(v.begin(), v.end(),
ostream_iterator<int>(cout, " "));
return 0;
}
Using accumulate()
The accumulate() function is typically used for summing values, but we can accumulate all the vector elements to a string and print it using a custom operation function.
C++
#include <bits/stdc++.h>
using namespace std;
int main(){
vector<int> v = { 1, 2, 3, 4, 5 };
// Acumulate vector to string
string res = accumulate(v.begin(), v.end(),
string(), [](string& s, int n) {
return s + (s.empty() ? "" : " ")
+ to_string(n);
});
// Print elements
cout << res;
return 0;
}
This method is more useful for processing the vector elements into a single output, such as concatenating them into a string or other custom operations.
By Overloading << operator
If there is a frequent need to print the vector elements, just overload the << operator as template function at global scope to work directly with the vector object.
C++
#include <bits/stdc++.h>
using namespace std;
// Overload << for vector
template <typename S>
ostream& operator<<(ostream& os,
const vector<S>& vector) {
// Printing all the elements using <<
for (auto i : vector)
os << i << " ";
return os;
}
int main() {
vector<int> v = {1,2,3,4,5};
// Print vector elements
cout << v;
return 0;
}
Similar Reads
How to Find Difference of Two Sorted Vectors in C++? In C++, vectors are containers similar to arrays but unlike arrays, vectors can change their size dynamically during the insertion or deletion of elements. In this article, we will learn how we can find the difference between two sorted vectors in C++. The difference of two vectors is a vector that
2 min read
How to Delete All Elements from a Vector in C++? In C++, you can delete all items from a vector to either clear its contents or free its memory. In this article, we will learn how to delete all items from a vector in C++.The recommended way to delete all items from a vector is by using the vector clear() function. Letâs take a look at a simple exa
2 min read
How to Check if a Vector Contains a Given Element in C++? In C++, a vector is a dynamic array that provides a resizable and efficient way to store elements. Vectors store their elements in contiguous memory locations, similar to arrays, which allows for efficient access and iteration.In this article, we will learn the different methods to check whether the
3 min read
How to Add Elements in a Vector in C++? In C++, vector provides several built-in methods to insert the elements and efficiency of the insertion depends on the position where the insertion takes place. In this article, we will learn different ways to insert elements into a vector in C++ and also compare their efficiency.The simplest way to
3 min read
How to Find the Mode of Vector in C++? In C++, a vector is a dynamic array that resizes itself when needed. It also allows random access to its elements. In this article, we will learn how to find the mode of a vector in C++. The mode of a vector is the element that highest number of occurrences in the vector. Example: Input: myVector =
2 min read