Professional Documents
Culture Documents
org/vector-in-cpp-stl/
How to write
an Interview
Experience?
Must Do
Coding
Questions
Company-wise
Must Do
Coding
Questions
Topic-wise
Difficulty
Levels
Basic
Easy
Medium
Hard
Expert
Popular Tags
Amazon,
Microsoft,
Dynamic
Programming,
Samsung
Click here for
more
1 of 8 2/3/18, 4:26 AM
Vector in C++ STL - GeeksforGeeks https://www.geeksforgeeks.org/vector-in-cpp-stl/
Interview
Preparation
Step by Step
Preparation
Company
Preparation
Top Topics
Company
Specific
Practice
Software
Design
Patterns
Placements
Preparation
Course
Interview
Corner
Recent
Interview
Experiences
GQ Home
Page
Quiz Corner
LMNs
Practice
Platform
What's New ?
Leaderboard !!
Topic-wise
Practice
Subjective
Problems
Difficulty Level
- School
2 of 8 2/3/18, 4:26 AM
Vector in C++ STL - GeeksforGeeks https://www.geeksforgeeks.org/vector-in-cpp-stl/
Difficulty Level
- Basic
Difficulty Level
- Easy
Difficulty Level
- Medium
Difficulty Level
- Hard
How to pick a
difficulty level?
Explore More...
Programming
Languages
C++
Java
Python
SQL
Important
Quick Links
School
Programming
Operating
Systems
DBMS
Computer
Networks
Engineering
Mathematics
Design
Patterns
Common
Interview
Puzzles
3 of 8 2/3/18, 4:26 AM
Vector in C++ STL - GeeksforGeeks https://www.geeksforgeeks.org/vector-in-cpp-stl/
Web
Technology
G-Facts
Computer
Graphics
Image
Processing
Project Ideas
Vectors are same as dynamic arrays with the ability to resize itself automatically when an
element is inserted or deleted, with their storage being handled automatically by the con-
tainer. Vector elements 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.Remov-
ing 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.
#include <iostream>
#include <vector>
4 of 8 2/3/18, 4:26 AM
Vector in C++ STL - GeeksforGeeks https://www.geeksforgeeks.org/vector-in-cpp-stl/
int main()
{
vector <int> g1;
vector <int> :: iterator i;
vector <int> :: reverse_iterator ir;
return 0;
Run on IDE
Capacity
1. size() – Returns the number of elements in the vector
2. max_size() – Returns the maximum number of elements that the vector can hold
3. capacity() – Returns the size of the storage space currently allocated to the vector ex-
pressed as number of elements
4. resize(size_type g) – Resizes the container so that it contains ‘g’ elements
5. empty() – Returns whether the container is empty
#include <iostream>
#include <vector>
int main()
{
vector <int> g1;
5 of 8 2/3/18, 4:26 AM
Vector in C++ STL - GeeksforGeeks https://www.geeksforgeeks.org/vector-in-cpp-stl/
return 0;
Run on IDE
Size : 5
Capacity : 8
Max_Size : 4611686018427387903
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector <int> g1;
return 0;
Run on IDE
6 of 8 2/3/18, 4:26 AM
Vector in C++ STL - GeeksforGeeks https://www.geeksforgeeks.org/vector-in-cpp-stl/
Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
Recommended Posts:
(Login to Rate)
7 of 8 2/3/18, 4:26 AM
Vector in C++ STL - GeeksforGeeks https://www.geeksforgeeks.org/vector-in-cpp-stl/
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link
here.
8 of 8 2/3/18, 4:26 AM