Different ways to print elements of vector
Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container. The elements of vectors are placed in contiguous storage so that they can be accessed and traversed using iterators. In vectors, data is inserted at the end. Inserting at the end takes differential time, as sometimes there may be a need of extending the array. Removing the last element takes only constant time because no resizing happens. Inserting and erasing at the beginning or in the middle is linear in time.
Different ways to print all elements of a Vector in C++
By using overloading << Operator: By overloading the << operator as template function at global scope, all the elements of the vector can be printed by iterating one by one.
Below is the C++ program to implement the above concept:
C++
#include <iostream>
#include <vector>
using namespace std;
template < typename S>
ostream& operator<<(ostream& os,
const vector<S>& vector)
{
for ( auto element : vector) {
os << element << " " ;
}
return os;
}
int main()
{
vector< int > A = { 10, 20, 30,
40, 50, 60 };
cout << A << endl;
return 0;
}
|
Printing in a comma-separated manner: By avoiding overloading of the << operator and by creating a separate function, a custom separator can be provided to print the contents of the vector
Below is the C++ program to implement the above approach:
C++
#include <iostream>
#include <vector>
using namespace std;
template < typename S>
void with_separator( const vector<S>& vec,
string sep = " " )
{
for ( auto elem : vec) {
cout << elem << sep;
}
cout << endl;
}
int main()
{
vector< int > int_vec{ 10, 20, 30,
40, 50, 60 };
with_separator(int_vec, ", " );
vector<string> str_vec{ "One" , "Two" ,
"Three" , "Four" ,
"Five" };
with_separator(str_vec, ", " );
return 0;
}
|
Output
10, 20, 30, 40, 50, 60,
One, Two, Three, Four, Five,
By using indexing: By using the index of the elements in the vector, all the elements can be printed.
Below is the C++ program to implement the above concept:
C++
#include <iostream>
#include <vector>
using namespace std;
template < typename S>
void using_index( const vector<S>& vector,
string sep = " " )
{
for ( int i = 0; i < vector.size(); i++) {
cout << vector[i] << sep;
}
cout << endl;
}
int main()
{
vector< int > int_vec{ 10, 20, 30,
40, 50, 60 };
using_index(int_vec);
return 0;
}
|
Printing all elements without for loop by providing element type: All the elements of a vector can be printed using an STL algorithm copy(). All the elements of a vector can be copied to the output stream by providing elements type while calling the copy() algorithm.
Below is the C++ program to implement the above approach:
C++
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main()
{
vector< int > vec_of_nums{ 10, 20, 30,
40, 50, 60 };
copy(vec_of_nums.begin(),
vec_of_nums.end(),
ostream_iterator< int >(cout, " " ));
return 0;
}
|
Using (experimental::make_ostream_joiner) without providing element type: In the Method-1 program, it is observed that while calling the copy() algorithm, the type of elements is specifically provided in the vector. But using C++ 17 experimental::make_ostream_joiner, it is possible to print all elements of a vector without specifying the type of elements in the vector while calling copy().
Below is the C++ program to implement the above approach:
C++
#include <experimental/iterator>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector< int > int_vec = { 10, 20, 30,
40, 50, 60 };
copy(int_vec.begin(),
int_vec.end(),
experimental::make_ostream_joiner(cout, " " ));
cout << endl;
return 0;
}
|
By using the Lambda function: The lambda function can be used on each element of the vector and inside the function, the element can be printed without providing the type of the element.
Below is the C++ program to implement the above approach:
C++
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
vector< int > int_vec{ 10, 20, 30,
40, 50, 60 };
for_each(int_vec.begin(),
int_vec.end(),
[]( const auto & elem) {
cout << elem << " " ;
});
return 0;
}
|
Last Updated :
03 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...