vector::at() and vector::swap() in C++ STL

30 Jun, 2022
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.
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.
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++
// CPP program to illustrate// Implementation of at() function#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;}
7
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.
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++
// CPP program to illustrate// Application of at() function#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);    // vector becomes 1, 2, 3, 4, 5, 6, 7, 8, 9     for (int i = 0; i < myvector.size(); i += 2) {         cout << myvector.at(i);        cout << " ";    }     return 0;}
1 3 5 7 9 
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.
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++
// CPP program to illustrate// Implementation of swap() function#include <iostream>#include <vector>using namespace std; int main(){    // vector container declaration    vector<int> myvector1{ 1, 2, 3, 4 };    vector<int> myvector2{ 3, 5, 7, 9 };     // using swap() function to swap    // elements of vector    myvector1.swap(myvector2);     // printing the first vector    cout << "myvector1 = ";    for (auto it = myvector1.begin();         it < myvector1.end(); ++it)        cout << *it << " ";     // printing the second vector    cout << endl         << "myvector2 = ";    for (auto it = myvector2.begin();         it < myvector2.end(); ++it)        cout << *it << " ";    return 0;}
myvector1 = 3 5 7 9 
myvector2 = 1 2 3 4 
If the size of the vectors differ:
C++
// CPP program#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;}
The vec1 contains: 200 200 200 200 200
The vec2 contains: 100 100 100
Let us see the differences in a tabular form -: 
soumya7
devgagrani123
shaista ambreen
7ckngmad
sweetyty
utkarshgupta110092
mayank007rawa
cpp-vector
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set in C++ Standard Template Library (STL)
vector erase() and clear() in C++
unordered_map in C++ STL
Inheritance in C++
Substring in C++
C++ Classes and Objects
Priority Queue in C++ Standard Template Library (STL)
Object Oriented Programming in C++
Sorting a vector in C++
Virtual Function in C++