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::at()
at() function is used reference the element present at the position given as the parameter to the function.
Syntax:
vectorname.at(position)
Parameters:
Position of the element to be fetched.
Returns:
Direct reference to the element at the given position.
Examples:
Input: myvector = 1, 2, 3
myvector.at(2);
Output: 3
Input: myvector = 3, 4, 1, 7, 3
myvector.at(3);
Output: 7
Errors and Exceptions
- If the position is not present in the vector, it throws out_of_range.
- It has a strong no exception throw guarantee otherwise.
Time Complexity – Constant O(1)
C++
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector< int > myvector;
myvector.push_back(3);
myvector.push_back(4);
myvector.push_back(1);
myvector.push_back(7);
myvector.push_back(3);
cout << myvector.at(3);
return 0;
}
|
Applications:
Given a vector of integers, print all the integers present at even positions.
Input: 1, 2, 3, 4, 5, 6, 7, 8, 9
Output: 1 3 5 7 9
Explanation - 1, 3, 5, 7 and 9 are at position 0, 2, 4, 6 and 8 which are even
Algorithm
- Run a loop till the size of the vector.
- Check if the position is divisible by 2, if yes, print the element at that position.
C++
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector< int > myvector;
myvector.push_back(1);
myvector.push_back(2);
myvector.push_back(3);
myvector.push_back(4);
myvector.push_back(5);
myvector.push_back(6);
myvector.push_back(7);
myvector.push_back(8);
myvector.push_back(9);
for ( int i = 0; i < myvector.size(); i += 2) {
cout << myvector.at(i);
cout << " " ;
}
return 0;
}
|
vector::swap()
This function is used to swap the contents of one vector with another vector of same type and sizes of vectors may differ.
Syntax:
vectorname1.swap(vectorname2)
Parameters:
The name of the vector with which
the contents have to be swapped.
Result:
All the elements of the 2 vectors are swapped.
Examples:
Input: myvector1 = {1, 2, 3, 4}
myvector2 = {3, 5, 7, 9}
myvector1.swap(myvector2);
Output: myvector1 = {3, 5, 7, 9}
myvector2 = {1, 2, 3, 4}
Input: myvector1 = {1, 3, 5, 7}
myvector2 = {2, 4, 6, 8}
myvector1.swap(myvector2);
Output: myvector1 = {2, 4, 6, 8}
myvector2 = {1, 3, 5, 7}
Errors and Exceptions
- It throws an error if the vector is not of the same type.
- It has a basic no exception throw guarantee otherwise.
Time Complexity – Constant O(1)
C++
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector< int > myvector1{ 1, 2, 3, 4 };
vector< int > myvector2{ 3, 5, 7, 9 };
myvector1.swap(myvector2);
cout << "myvector1 = " ;
for ( auto it = myvector1.begin();
it < myvector1.end(); ++it)
cout << *it << " " ;
cout << endl
<< "myvector2 = " ;
for ( auto it = myvector2.begin();
it < myvector2.end(); ++it)
cout << *it << " " ;
return 0;
}
|
Output
myvector1 = 3 5 7 9
myvector2 = 1 2 3 4
If the size of the vectors differ:
C++
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector< int > vec1{ 100, 100, 100 };
vector< int > vec2{ 200, 200, 200, 200, 200 };
vec1.swap(vec2);
cout << "The vec1 contains:" ;
for ( int i = 0; i < vec1.size(); i++)
cout << ' ' << vec1[i];
cout << '\n' ;
cout << "The vec2 contains:" ;
for ( int i = 0; i < vec2.size(); i++)
cout << ' ' << vec2[i];
cout << '\n' ;
return 0;
}
|
Output
The vec1 contains: 200 200 200 200 200
The vec2 contains: 100 100 100
Let us see the differences in a tabular form -:
|
vector::at() |
vector::swap() |
1. |
It is used to return a reference to the element at position n in the vector. |
It is used to swap the elements of one vector with the elements of another vector. |
2. |
Its syntax is -: reference at (size_type n); |
Its syntax is -: swap (vector& x); |
3. |
It only takes one parameter which is the position of an element in the container. |
It only takes one parameter which is the vector that we want to swap. |
4. |
Its complexity is constant. |
It does not have any return value. |
5. |
Its iterator validity does not change. |
Its complexity is constant. |
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!