Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

STL - Vector - HackerEarth

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

8/17/2016 STLVector|HackerEarth

Signup and contribute to Notes. Start Now


3

LIVE EVENTS
Notes

STL - Vector
2 Vector C++ STL

Vector is one of various useful containers defined under Standard Template Library(STL).
We can say that Vector is an advanced version of array in C++ . Though it contains some
good features than array.

Vector Header File ,

#include<vector>

Vector Declaration , Syntax : `

vector<data_type>identifier(size,initial_value);//Heresize&
initial_valueareoptional

Example :

vector<int>A(5,1);//vectorofsize=5withallelementsinvector
initialisedwithvalue=1

Insertion in Vector , There are two ways for inserting an element in a Vector , either you
can use push_back() or insert(index,value) , insert() will insert value at index give ,
and push_back() will insert value after the last element which is already in vector. And
insert() will insert the value as well as it will swap all the preceding elements by the
number of elements inserted

Example :

A.push_back(5);//A={1,1,1,1,1,5},
vectorcanexpanditssize

https://www.hackerearth.com/notes/stlvector/ 1/4
8/17/2016 STLVector|HackerEarth

A.insert(1,6);//A={1,6,1,1,1,1,5}

Resizing the vector :

A.resize(10);//nowthevector'A'hassize=10

Erase all the values of vector :

A.erase();//EraseallvaluesofvectorA

Erase values of vector lie in a given interval :

A.erase(1,5);//Erasevaluesfromindex=1to5

Check if any vector is empty :

if(A.empty()){//Statements}

Importing value of any array into a vector , let an array array like

intarray[]={1,2,3,4,5};

vector<int>A(array,array+5);//Itwillmakevectorelements
A={1,2,3,4,5}

Size of vector :

intsize=A.size();//itgivessizeofvector'A',i.e.numberof
elementspresentinvector'A'

Creating Matrix with help of Vector ,

vector<int,vector<int>>Mat(M,vector<int>N);//Matrixofsize
M*N

Many <algorithm> library function can be implemented on vector(s) , that I will explain
in <algorithm> Note

https://www.hackerearth.com/notes/stlvector/ 2/4
8/17/2016 STLVector|HackerEarth

Tweet

COMMENTS (0)

Login/Signup to Comment

AUTHOR

Bhavesh Kumar
Problem Setter at HackerE
Jaipur
1 note

TRENDING NOTES

Bokeh | Interactive Visualization Library |


Use Graph with Django Template
written by Prateek Kumar

Bokeh | Interactive Visualization Library |


Graph Plotting
written by Prateek Kumar

Python Diaries chapter 2


written by Divyanshu Bansal

Python Diaries chapter 1


written by Divyanshu Bansal

Some Interesting Facts about Functions in C


written by Bhavini Sarkar
more ...

ABOUT US HACKEREARTH DEVELOPERS

Blog API AMA

Engineering Blog Chrome Extension Code Monk

Updates & Releases CodeTable Judge Environment

Team HackerEarth Academy Solution Guide

https://www.hackerearth.com/notes/stlvector/ 3/4
8/17/2016 STLVector|HackerEarth

Careers Developer Profile Problem Setter Guide

In the Press Resume Practice Problems

Get Badges HackerEarth Challenges

Campus Ambassadors College Challenges

Get Me Hired College Ranking

Privacy Organise Hackathon

Terms of Service Hackathon Handbook

Competitive Programming

Open Source

EMPLOYERS REACH US

Developer Sourcing

Lateral Hiring

Campus Hiring

Hackathons

FAQs Map data 2016 Google


Ground Floor, Salarpuria Business Center,
Customers
4th B Cross Road, 5th A Block,
Koramangala Industrial Layout,
Bangalore, Karnataka 560095, India.

contact@hackerearth.com
+91-80-4155-4695
+1-650-461-4192

2016 HackerEarth

https://www.hackerearth.com/notes/stlvector/ 4/4

You might also like