
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to print out the contents of a vector in C++?
Vectors are similar to the dynamic arrays but vectors can resize. They are sequence containers that can change their size according to the insertion or deletion of elements. Containers are the objects which holds the data of same type.
Vectors may allocate some extra storage for the future growth of elements in the vector. Its elements are stored in the contiguous memory. The data is entered at the end of vector.
In this article, we will show you how to print the contents of a vector in C++.
Example Scenario:
std::vector<int> vec = {10, 20, 30, 40, 50};
You can use a loop or an iterators to print each element, and the output will be:
10 20 30 40 50
Our task is to explain different methods to print vector elements in C++, so you can choose the one that works best for you.
C++ Programs to Print Contents of a Vector
We will cover the following approaches for printing a vector:
- Using Traditional for loop
- Using Range-Based for loop
- Using std::for_each() with Lambda Function
- Using Iterators
- Using std::copy() Function
Using Traditional for loop
In this approach, we use a regular for loop to go through the vector and print each element one by one. It's a simple and easy-to-understand method.
Example
Here's the code where we define a vector and use a for loop to print each element, followed by a space. After the loop finishes, we print a new line for clean output.
#include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; // Loop through the vector and print each element std::cout << "The elements in the vector are: "; for (int i = 0; i < vec.size(); ++i) { std::cout << vec[i] << " "; } std::cout << std::endl; // Print a new line after output return 0; }
Output
Below is the output of the above code, where each element of the vector is printed separated by a space.
The elements in the vector are: 1 2 3 4 5
Time Complexity: O(n), where n is the size of the vector.
Space Complexity O(1), as we use only a few variables for iteration.
Using Range-Based for loop
A range-based for loop is a simpler and more modern way to iterate over a vector. It removes the need to manually manage an index, making the code cleaner and easier to read.
Example
In this example, we use a range-based for loop to print each element of the vector. This loop automatically goes through all elements in the vector, without the need for an index, and prints each one.
#include <iostream> #include <vector> int main() { std::vector<int> vec = {10, 20, 30, 40, 50}; // Print the elements of the vector std::cout << "The elements in the vector are: "; for (int num : vec) { std::cout << num << " "; } std::cout << std::endl; // Print a new line after output return 0; }
Output
Here's the output, displaying all vector elements using a range-based for loop.
The elements in the vector are: 10 20 30 40 50
Time Complexity: O(n), where n is the number of elements in the vector.
Space Complexity: O(1), as only a temporary variable (num) is used.
Using std::for_each() with Lambda Function
In this approach, we use std::for_each() with a lambda function to print each element of the vector. The lambda function defines the action to be performed on each element, which is to output it to the console.
Example
The code below shows how std::for_each() and a lambda function are used to print all the elements in the vector.
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> vec = {7, 14, 21, 28, 35}; // Print the elements of the vector using for_each std::cout << "The elements in the vector are: "; std::for_each(vec.begin(), vec.end(), [](int num) { std::cout << num << " "; }); return 0; }
Output
Here is the output where we use std::for_each() and a lambda to print each vector element.
The elements in the vector are: 7 14 21 28 35
Time Complexity: O(n), where n is the size of the vector.
Space Complexity: O(1), as no additional space is used.
Using Iterators
In this approach, we use Iterators to loop through the vector. Iterators are a flexible tool that allows us to traverse elements in any C++ container, not just vectors.
Example
In this example, we use an iterator to go through the vector and print its elements.
#include <iostream> #include <vector> int main() { std::vector<int> vec = {8, 16, 24, 32, 40}; // Print the elements of the vector using an iterator std::cout << "The elements in the vector are: "; for (std::vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) { std::cout << *it << " "; } return 0; }
Output
Below is the output where we use iterators to print each element of the vector.
The elements in the vector are: 8 16 24 32 40
Time Complexity: O(n), where n is the number of elements in the vector.
Space Complexity: O(1), as iterators use constant space.
Using std::copy() Function
In this approach, we use the std::copy() function with an output iterator, like std::ostream_iterator, to print the elements of the vector efficiently in just one line of code. This method is more concise and functional.
Example
In this example, we use std::copy() with an output iterator to print the vector's elements to std::cout, making the process simple and clean.
#include <iostream> #include <vector> #include <iterator> #include <algorithm> int main() { std::vector<int> vec = {2, 4, 6, 8, 10}; // Print the elements of the vector using ostream_iterator std::cout << "The elements in the vector are: "; std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << std::endl; return 0; }
Output
Here is the output where we use std::copy() and std::ostream_iterator to print the vector.
The elements in the vector are: 2 4 6 8 10
Time Complexity: O(n), where n is the number of elements in the vector.
Space Complexity: O(1), no extra space is used except for the iterator.
Conclusion
In this article, we looked at different ways to print a vector in C++, from basic loops to advanced functions like std::for_each(). Each method has its own benefits in terms of readability and efficiency. Understanding these options helps you pick the best one for your needs.