You are on page 1of 8

Vector in C++ STL - GeeksforGeeks https://www.geeksforgeeks.

org/vector-in-cpp-stl/

GeeksforGeeks Custom Search


A computer science portal for geeks
Practice GATE CS Placements Videos Contribute
Login/Register

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

Vector in C++ STL


2
Vector

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.

Certain functions are associated with vector :


Iterators
1. begin() – Returns an iterator pointing to the first element in the vector
2. end() – Returns an iterator pointing to the theoretical element that follows last element
in the vector
3. rbegin() – Returns a reverse iterator pointing to the last element in the vector (reverse
beginning). It moves from last to first element
4. rend() – Returns a reverse iterator pointing to the theoretical element preceding the
first element in the vector (considered as reverse end)

#include <iostream>
#include <vector>

using namespace std;

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;

    for (int i = 1; i <= 5; i++)


        g1.push_back(i);

    cout << "Output of begin and end\t:\t";


    for (i = g1.begin(); i != g1.end(); ++i)
        cout << *i << '\t';

    cout << endl << endl;


    cout << "Output of rbegin and rend\t:\t";
    for (ir = g1.rbegin(); ir != g1.rend(); ++ir)
        cout << '\t' << *ir;

    return 0;

Run on IDE

The output of the above program is :

Output of begin and end : 1 2 3 4 5

Output of rbegin and rend : 5 4 3 2 1

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>

using namespace std;

int main()
{
    vector <int> g1;

    for (int i = 1; i <= 5; i++)


        g1.push_back(i);

    cout << "Size : " << g1.size();

5 of 8 2/3/18, 4:26 AM
Vector in C++ STL - GeeksforGeeks https://www.geeksforgeeks.org/vector-in-cpp-stl/

    cout << "\nCapacity : " << g1.capacity();


    cout << "\nMax_Size : " << g1.max_size();

    return 0;

Run on IDE

The output of the above program is :

Size : 5
Capacity : 8
Max_Size : 4611686018427387903

Accessing the elements


1. reference operator [g] – Returns a reference to the element at position ‘g’ in the vector
2. at(g) – Returns a reference to the element at position ‘g’ in the vector
3. front() – Returns a reference to the first element in the vector
4. back() – Returns a reference to the last element in the vector

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

int main()
{
    vector <int> g1;

    for (int i = 1; i <= 10; i++)


        g1.push_back(i * 10);

    cout << "Reference operator [g] : g1[2] = " << g1[2];


    cout << endl;
    cout << "at : g1.at(4) = " << g1.at(4);
    cout << endl;
    cout << "front() : g1.front() = " << g1.front();
    cout << endl;
    cout << "back() : g1.back() = " << g1.back();
    cout << endl;

    return 0;

Run on IDE

The output of the above program is :

6 of 8 2/3/18, 4:26 AM
Vector in C++ STL - GeeksforGeeks https://www.geeksforgeeks.org/vector-in-cpp-stl/

Reference operator [g] : g1[2] = 30


at : g1.at(4) = 50
front() : g1.front() = 10
back() : g1.back() = 100

Click here for Set 2 of Vectors.


Please write comments if you find anything incorrect, or you want to share more informa-
tion about the topic discussed above

The Kendo UI library provides everything Pass Your JAVA


100% Free Exam Dumps &
Kendo UI for React
you need to integrate with React out of the
box.
PrepAway.com
Exams
Latest Questions

GATE CS Corner Company Wise Coding Practice

C++ cpp-containers-library STL Login to Improve this Article

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:

Modifiers for Vector in C++ STL


The C++ Standard Template Library (STL)
List in C++ Standard Template Library (STL)
Ways to copy a vector in C++
Map in C++ Standard Template Library (STL)
Boost.Lexical_Cast in C++
forward_list::emplace_front() in C++STL
Difference between strncmp() and strcmp in C/C++
Early binding and Late binding in C++
stack::emplace() in C++ STL

(Login to Rate)

Average Difficulty : 2/5.0


2 Based on 42 vote(s)
Add to TODO List
Mark as DONE
Basic Easy Medium Hard Expert

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.

Load Comments Share this post!

@geeksforgeeks, Some rights reserved Contact Us! About Us! Careers!


Privacy Policy

8 of 8 2/3/18, 4:26 AM

You might also like