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

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);
Outpu: 3

Input: myvector = 3, 4, 1, 7, 3
         myvector.at(3);
Output: 7

Errors and Exceptions  

  1. If the position is not present in the vector, it throws out_of_range.
  2. It has a strong no exception throw guarantee otherwise.
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
chevron_right

Output

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  

  1. Run a loop till the size of the vector.
  2. Check if the position is divisible by 2, if yes, print the element at that position.
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
chevron_right

Output
1 3 5 7 9 

 

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  

  1. It throws an error if the vector is not of the same type.
  2. It has a basic no exception throw guarantee otherwise.
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
chevron_right

Output
myvector1 = 3 5 7 9 
myvector2 = 1 2 3 4 

If the size of the vectors differ:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
chevron_right

Output
The vec1 contains: 200 200 200 200 200
The vec2 contains: 100 100 100

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.




Article Tags :
C++
Practice Tags :