iter_swap() in C++

iter_swap(): This function is available in the file stl_algobase.h. This function swaps the contents of two iterators. This function does not returns anything (it is of void return type)

Parameters: Two parameters are required for this function.

 __a – An iterator.

 __b – Another iterator. 

Note: This function swaps the values pointed to by two iterators, not the iterators themselves.

Syntax:

iter_swap(__a, __b)

For Example:

vec = {1,2,3,4}

iter_swap(vec.begin(),vec.begin()+2) = > vec = {3,2,1,4}

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<intvec = {1234};

    //swaps first and third
    iter_swap(vec.begin(), vec.begin() + 2);

       for (int i = 0i < vec.size(); i++)
        cout << vec[i] << " ";
    return 0;
}


No comments:

Post a Comment