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

9/2/2017 Vector in C++ STL - GeeksforGeeks

GeeksforGeeks Get It now


Custom Search
A computer science portal
Try for geeks
all-new Acrobat DC desktop free for 30 days

Practice GATE CS Placements Videos Contribute


Login/Register

Vector in C++ STL


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 container. 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.Removing 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;

int main()
{
vector <int> g1;
vector <int> :: iterator i;

file:///C:/Users/user/Downloads/Vector%20in%20C++%20STL%20-%20GeeksforGeeks.html 1/7
9/2/2017 Vector in C++ STL - GeeksforGeeks

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;

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 expressed 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();


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

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

return 0;

file:///C:/Users/user/Downloads/Vector%20in%20C++%20STL%20-%20GeeksforGeeks.html 2/7
9/2/2017 Vector in C++ STL - GeeksforGeeks

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;

The output of the above program is :

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


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

file:///C:/Users/user/Downloads/Vector%20in%20C++%20STL%20-%20GeeksforGeeks.html 3/7
9/2/2017 Vector in C++ STL - GeeksforGeeks

Click here for Set 2 of Vectors.


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

GATE CS Corner Company Wise Coding Practice

C++ cpp-containers-library STL

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)
Range-based for loop in C++
std::lexicographical_compare() in C++STL

div() function in C++


std::string::at in C++
file:///C:/Users/user/Downloads/Vector%20in%20C++%20STL%20-%20GeeksforGeeks.html 4/7
9/2/2017 Vector in C++ STL - GeeksforGeeks

exit() vs _Exit() in C and C++

(Login to Rate and Mark)

Average Difficulty : 2.3/5.0


2.3 Based on 24 vote(s)
Add to TODO List
Mark as DONE

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

file:///C:/Users/user/Downloads/Vector%20in%20C++%20STL%20-%20GeeksforGeeks.html 5/7
9/2/2017 Vector in C++ STL - GeeksforGeeks

file:///C:/Users/user/Downloads/Vector%20in%20C++%20STL%20-%20GeeksforGeeks.html 6/7
9/2/2017 Vector in C++ STL - GeeksforGeeks

file:///C:/Users/user/Downloads/Vector%20in%20C++%20STL%20-%20GeeksforGeeks.html 7/7

You might also like