The Wayback Machine - https://web.archive.org/web/20240527123604/https://www.geeksforgeeks.org/vectorat-vectorswap-c-stl/
Open In App

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

Last Updated : 30 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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  

  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.

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


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.

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


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.

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


Output

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


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.


Previous Article
Next Article

Similar Reads

Difference between std::swap and std::vector::swap
The std::swap is a general function used to exchange the given values whereas the std::vector::swap is a specialized function that can swap all the contents of two different vector containers.Below are some major key differences between std::swap and std::vector::swap, std::swapstd::vector::swapThe std::swap() is a built-in function in C++ STL whic
3 min read
vector::front() and vector::back() 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::front() This function can be used to fetch the first element of a vector container.Syntax : vectorname.front() Parameters : No value is needed to pass as
3 min read
vector::empty() and vector::size() in C++ STL
Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::empty() The empty() function is used to check if the vector container is empty or not.Syntax : vectorname.empty() Parameters : No parameters are p
5 min read
vector::push_back() and vector::pop_back() 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::push_back() push_back() function is used to push elements into a vector from the back. The new value is inserted into the vector at the end, after the cur
4 min read
vector::operator= and vector::operator[ ] 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::operator= This operator is used to assign new contents to the container by replacing the existing contents. It also modifies the size according to the new
4 min read
vector::begin() and vector::end() in C++ STL
Vectors are the 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::begin() begin() function is used to return an iterator pointing to the first element of the vector container. begin() function returns a bidirectional
3 min read
vector :: cbegin() and vector :: cend() in C++ STL
Vectors are known as dynamic arrays which can change its size automatically when an element is inserted or deleted. This storage is maintained by container. vector::cbegin() The function returns an iterator which is used to iterate container. The iterator points to the beginning of the vector.Iterator cannot modify the contents of the vector. Synta
2 min read
Initializing Vector using an Existing Vector in C++ STL
A vector is a type of container which can store objects of similar data type. Vector acts like a dynamic array where we can insert elements and the size of the array increases depending upon the elements inserted. Syntax: vector&lt;data_structure/type&gt; vector_name(size, item) To know more about vectors refer to vectors in C++. The 3D vector in C
5 min read
deque::at() and deque::swap() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::at()at() function is us
4 min read
array::fill() and array::swap() in C++ STL
Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::fill() This function is used to set a common value for all the elements of the array container. Syntax : arrayname.fill(value) Parameters : The value to be
3 min read
Article Tags :
Practice Tags :