• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer

Mr.CodeHunter

Programming and Code Solutions

  • Home
  • C++
  • About Us
  • Contact Us
  • Privacy Policy

How to print vector in c++?

August 29, 2020 by Admin 1 Comment

Vector in c++ is like a dynamic array, but it has the ability to resize whenever we are adding or deleting elements from vector. Vector is a type of Sequence container. Containers hold data of the same data types. Apart from sequence containers, there are different types of containers available in vector.

vector in c++

If we check internally, vectors are also dynamically allocated array which store elements. If more elements increase inside vector then it automatically increases the size of dynamically allocated array. Internally it increases its capacity. Expanding memory allocation every time is an expensive task, that’s why vector allocates a few capacities every time rather than single memory block allocation.

Sequence containers are vector, list, dequeue, arrays, and forward_list
Container Adaptors are Queue, priority_queue, stack
Associative containers are set, multiset, map, and multimap
Unordered associative containers are unordered_set, unordered_multiset, unordered_map and unordered_multimap

Elements of vectors are stored in continues memory location, so it is easy to print vector c++.

Print vector in C++

#include<iostream>
#include<vector>
using namespace std;

int main()
{
	vector<int> vData;
	vData.push_back(1);
	vData.push_back(2);
	vData.push_back(3);
	vData.push_back(4);

	vector<int>::iterator it;
	for (it = vData.begin(); it != vData.end(); it++)
	{
		cout << *it <<endl;
	}

	return 0;
}

Output:

print vector in c++

For using vector you need to include #include<vector> in header. For creating vector in c++ you have to use vector<datatype> objectName;

push_back(value) is used in vector for adding new value inside vector. push_back function will always add value at the end of vector. In case you want to add element in front of vector then there is push_front() function is also available.

How to print vector in c++ ?

For printing vector in c++ you have to create iterator which will iterate throughout the vector and print element one by one. you can create iterator by given syntax.

vector<DataType>::Iterator ObjectName;

Now you can use iterator object in for loop for iterating first element to last element. We can assign starting as vector begin() function and end as vector end() function. So we will continue for loop untill iterator is not end().

For printing value of vector in c++ we use pointer of iterator object. *it will give value of vector which we can easily print.

Print vector in c++ using auto keyword (c++ 11 feature)

	for (auto it = vData.begin(); it != vData.end(); it++)
	{
		cout << *it << endl;
	}

It will also give similar output and print vector in c++. Using auto keyword we will not require to declare iterator because auto will automatically identify which type require to hold value. So here it will automatically identify that it will require vector<int>::iterator datatype. Given auto keyword feature is introduced in c++ 11.

C++ print vector in reverse order

After looking above example you must have question someone want c++ print vector in reverse order then ? Yes, STL (Standard Template Library) is providing reverse traversal by rbegin() and rend() functions.

	for (auto it = vData.rbegin(); it != vData.rend(); it++)
	{
		cout << *it << endl;
	}

Output :

c++ print vector in reverse

Conclusion:

If you want to print vector in C++ then you can use iterator and traverse loop and print via pointer of iterator object. If your project/IDE is supporting c++ 11 then i will recommend you to use auto keyword for traversing for loop and display value.

Filed Under: All C++, C++, C++11

Reader Interactions

Comments

  1. M106 says

    June 3, 2021 at 2:11 am

    Many thanks for discussing this fantastic written content on your website. I came across it on google. I may check to come back once you post additional aricles.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Search here

Social Media

  • Facebook
  • YouTube

SEE MORE

Fibonacci sequence c++

Fibonacci Sequence c++ is a number sequence which created by sum of previous two numbers. First two number of series are 0 and 1. And then using these two number Fibonacci series is create like 0, 1, (0+1)=1, (1+1)=2, (2+1)=3, (2+3)=5 …etc Displaying Fibonacci Series in C++ ( without recursion) Output: From given output we […]

map c++

C++ Map [Learn by Example]

C++ map is part of Standard Template Library (STL). It is type of Associative container. Map in c++ is used to store unique key and it’s value in data structure. But if you want to store non-unique key value then you can use Multi Map in c++. Let us first understand in detail what is […]

how to copy paste in turbo c++ ?

There are many different C++ IDE are available but still many students are using Turbo c++ for learning c/c++ programming languages. During using Turbo c++ if you are beginner you will be confuse for how to copy and paste in turbo c++ or if you have already copy some content and you want to paste […]

C++

return 0 c++

There are two different scenario return statement is used inside c++ programming. We can use return 0 c++ inside main() function or other user defined functions also. But both have different meanings. return 0 c++ used inside Main function return 0 c++ used inside other user defined function What is meaning of return 0 and […]

C++

c++ expected a declaration [ SOLVED]

When any function or statement is not in scope or we have used wrong syntax then possibly you will get error for c++ expected a declaration in your code. Main reasons for errors are: Incorrect use/declaration inside namespace Statements are added out of scope Required statement need to add inside main/function Solution-1 | Expected a […]

C++

c++ cannot open source file “errno.h” [SOLVED]

Normally you will face c++ cannot open source file “errno.h” in MS Visual Studio c++ projects. These are some solutions to remove opening errors for “errno.h” file. I got the errors to go away by installing the Windows Universal CRT SDK component, which adds support for legacy Windows SDKs. You can install this using the […]

Footer

DISCLAIMER

The information contained on https://www.mrcodehunter.com is for general information purposes only. We assumes no responsibility for errors or omissions in the contents on the Service.

SITEMAP XML

Sitemap

Recent

  • Fibonacci sequence c++
  • C++ Map [Learn by Example]
  • how to copy paste in turbo c++ ?
  • return 0 c++
  • c++ expected a declaration [ SOLVED]

Search

Copyright © 2025 ยท Mr Code Hunter