0% found this document useful (0 votes)
3 views

C++ Vector

A C++ vector is a dynamic array that automatically resizes itself when elements are added. It stores elements contiguously in memory and allocates memory as needed at runtime. Common vector functions include push_back() to add elements, at() to access elements, and pop_back() to remove the last element.

Uploaded by

subham saklani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C++ Vector

A C++ vector is a dynamic array that automatically resizes itself when elements are added. It stores elements contiguously in memory and allocates memory as needed at runtime. Common vector functions include push_back() to add elements, at() to access elements, and pop_back() to remove the last element.

Uploaded by

subham saklani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

C++ Vector

C++ Vector
• A vector is a sequence container class that implements dynamic array, means
size automatically changes when appending elements. A vector stores the
elements in contiguous memory locations and allocates the memory as needed
at run time.

• Difference between vector and array


An array follows static approach, means its size cannot be changed during run
time while vector implements dynamic array means it automatically resizes itself
when appending elements.

Syntax
Consider a vector 'v1’.
Syntax would be:
vector<object_type> v1;
Rakesh Sharma
#include<iostream>
#include<vector>
int main()
{
vector<string> v1;
v1.push_back(“Rakesh ");
v1.push_back(“Sharma");
for( vector<string>::iterator itr=v1.begin(); itr!=v1.end(); itr++)
cout<<*itr;
return 0;
}
Rakesh Sharma
Output: Rakesh Sharma
C++ Vector Functions
Function Description
at() It provides a reference to an element.

back() It gives a reference to the last element.

front() It gives a reference to the first element.

swap() It exchanges the elements between two vectors.

push_back() It adds a new element at the end.


pop_back() It removes a last element from the vector.

empty() It determines whether the vector is empty or not.

insert() It inserts new element at the specified position.

erase() It deletes the specified element.


Rakesh Sharma
C++ Vector at()
It gives a reference to an element.
Syntax
Consider a vector v and k is the position.
Syntax would be:
vector<object_type> v;
v.at(k) ;
Parameter
k: k defines the position of element which is to be returned by at()
function.
Return value
It returns the element of specified position.

Rakesh Sharma
#include<iostream>
#include<vector>
void main()
{
vector <int> v1{1,2,3,4};
for(int i=0;i<v1.size();i++)
cout<<v1.at(i);
}

Output:
1234
Rakesh Sharma
C++ Vector push_back()
• This function adds a new element at the end of the vector.
Syntax
Consider a vector v and 'k' is the value.
Syntax would be:
v.push_back(k)
Parameter
k: k is the value which is to be inserted at the end of the vector.

Rakesh Sharma
#include<iostream>
#include<vector>
void main()
{
vector<char> v;
v.push_back(‘m');
v.push_back(‘a');
v.push_back(‘n');
v.push_back(‘0');
v.push_back(‘j');
for(int i=0; i<v.size(); i++)
cout<<v[i];
}
Output: manoj
Rakesh Sharma
#include<iostream>
#include<vector>
void main()
{
vector<int> v;
v.push_back( 1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
for(int i=0; i<v.size(); i++)
cout<<v[i];
}
Output:1 2 3 4 5

Rakesh Sharma
Vector pop_back()

• It deletes the last element and reduces the size of the vector by one.

Syntax
Consider a vector v.
Syntax would be:
v.pop_back();
Parameter
It does not contain any parameter.

Rakesh Sharma
#include<iostream>
#include<vector>
int main()
{
vector<string> v{"welcome","to",“Amrapali",“University"};
cout<<"Initial string is :";
for(inti=0;i<v.size();i++)
cout<<v[i]<<" ";
cout<<'\n';
cout<<"After deleting last string, string is :";
v.pop_back();
for(int i=0;i<v.size();i++)
cout<<v[i]<<" ";
return 0;
}
Rakesh Sharma
• Output:

• Initial string is : welcome to Amrapali University


• After deleting last string, string is : welcome to Amrapali

Rakesh Sharma

You might also like