
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to print out the contents of a vector in C++?
Vectors are similar to the dynamic arrays but vectors can resize. Vectors 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. Vector elements are stored in the contiguous memory. The data is entered at the end of vector.
Here is an example to print the contents of a vector in C++ language,
Example
#include<iostream> #include<vector> void print(std::vector <int> const &a) { std::cout << "The vector elements are : "; for(int i=0; i < a.size(); i++) std::cout << a.at(i) << ' '; } int main() { std::vector<int> a = {2,4,3,5,6}; print(a); return 0; }
Output
Here is the output −
The vector elements are : 2 4 3 5 6
In the above program, function print() contains the code to get the elements of vector. In the for loop, size of vector is calculated for the maximum number of iterations of loop and using at(), the elements are printed.
for(int i=0; i < a.size(); i++) std::cout << a.at(i) << ' ';
In the main() function, the elements of vector are passed to print them.
std::vector<int> a = {2,4,3,5,6}; print(a);
- Related Articles
- Operator overloading in C++ to print contents of vector, map, pair ..
- Print contents of a file in C
- How to print the contents of array horizontally using C#?
- How to print the vector elements vertically in R?
- How to print a factor vector without levels in R?
- How to Copy the entire contents of a directory in C#?
- How to append a vector in a vector in C++?
- How to initialize a vector in C++?
- Clear out all of the Vector elements in Java
- How to save the contents of a Textbox in Tkinter?
- How to shuffle a std::vector in C++
- How to sum up elements of a C++ vector?
- How to find the maximum element of a Vector using STL in C++?
- How to find the sum of elements of a Vector using STL in C++?
- How to retrieve the contents of a text field in JavaFX?
