Table of Contents
This article discusses the vector and how to print vector in C++.
Vectors are similar to dynamic arrays in that they can resize themselves automatically when an element is added or removed. The container automatically handles the size of the vector.
Elements in a vector are stored in contiguous memory locations. You can traverse these elements using indices or by iterators.
Print Vector in C++ Using Indices
You can access the elements of a vector using indices within the square brackets ([]).
To print the vector,
- Start traversing the vector from the first element using a loop. Start at 0-index.
- Print the current element.
Let us see the code that uses for-loop to print the vector using indices.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <vector> using namespace std; int main() { vector<int> vec{10, 20, 30, 40, 50, 60}; for (int i=0; i<vec.size();i++) { cout<<vec[i]<<" "; } return 0; } |
Notice the size()
function in the loop. It returns the number of elements in the vector.
Output:
Further reading:
Print Vector in C++ Using Range-Based for Loop
C++11 added the range-based for loop. It is more compact than the traditional for loop in the sense that you do not have to manually specify starting and ending iterators or indices.
The range-based for loop automatically traverses all the elements of the vector.
Let us see the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <vector> using namespace std; int main() { vector<int> v={1,2,3,4,5,6}; for (int x : v) { cout<<x<<" "; } return 0; } |
Output:
Print Vector in C++ Using for_each() Function
The for_each()
function facilitates the tasks where you need to apply a function to all the elements within a range of a container such as a vector.
You can print the elements of the vector by calling the for_each()
and passing the starting and ending iterators to it as arguments.
Note that you must also create a function that takes an element of your vector’s data type and print it. This function is passed to the for_each()
function as the third argument.
The definition of the for_each()
is given below.
1 2 3 4 |
template <class InputIterator, class Function> Function for_each (InputIterator first, InputIterator last, Function fn); |
Let us see the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <vector> #include <algorithm> using namespace std; void fun(int num) { cout<<num<<" "; } int main() { vector<int> v= {3,1,5,7,2,9}; for_each(v.begin(), v.end(), fun); return 0; } |
Output:
Print Vector in C++ Using Iterator
Iterators are similar to pointers and point to a specific memory location of the vector. You can use the following methods to retrieve iterators and use them to traverse the vector.
begin()
: Returns an iterator pointing to the vector’s first element.end()
: Returns an iterator pointing to the theoretical element following the vector’s last element.
Let us see the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> v = {1,1,1,7,2,9}; for(auto it=v.begin(); it!=v.end(); it++) { cout<<*it<<" "; } return 0; } |
Output:
You can note the use of the auto
keyword. To know more about this keyword, you can visit here.
Print Vector in C++ by Overloading the Insertion Operator (<<)
The insertion operator (<<)
in C++ is used to direct the objects to a stream. Generally, you might have used it to print the data to the console output.
However, C++ provides a facility to overload the insertion operator (<<)
. Therefore, you can make use of this functionality to print the elements of the vector.
You would need to overload the insertion operator (<<) by defining your own ostream
class operator (<<)
to print the vector.
You can overload the ostream
operator to accept the arguments as given below.
1 2 3 |
ostream &operator <<(ostream &stream, const vector<int> &v) |
Let us see the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #include <vector> #include <algorithm> using namespace std; ostream &operator <<(ostream &stream, const vector<int> &v) { for(auto const &it: v) { stream<<it<<" "; } return stream; } int main() { vector<int> v = {1,2,5,7,2,9}; cout<<v; return 0; } |
Output:
Print Vector in C++ Using copy() Function
The copy()
function is C++ copies all the data elements of a container within a range to an output iterator.
You can create an output iterator to the console output using the ostream_iterator()
function. To do so you need to pass the cout
and the separator (“ ”) to the function.
Finally, you can pass the starting iterator and ending iterator of vector and output iterator to the copy()
function. It prints all the elements of the vector.
The definition of the copy()
is given below.
1 2 3 4 |
template <class InputIterator, class OutputIterator> OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result); |
Let us see the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <vector> #include <algorithm> #include <iterator> using namespace std; int main() { vector<int> v = {1,2,2,2,2,9}; ostream_iterator<int> out_itr (cout, " "); copy(v.begin(), v.end(), out_itr); return 0; } |
Output:
Conclusion
You have seen several different methods to print the vector elements in C++. All of these methods apply best to different situations. However, using copy()
lets you avoid the loop.
On the other hand, if you need to print the vector too frequently, it would be better to overload the insertion operator (<<)
. Or you also define a function that prints the vector using any of the methods given in the article.
This is all about how to print vector in C++.
Hope you have enjoyed the article. Stay tuned for more such articles. Happy Learning!
References
- https://www.cplusplus.com/reference/algorithm/copy/
- https://www.cplusplus.com/reference/algorithm/for_each/
- https://www.cplusplus.com/reference/iterator/ostream_iterator/