C++ pair swap non-member function

This version of swap function is a non-member function.And it accepts two arguments of pair type object.

template<typename T1, typename T2>inline void swap(pair< T1, T2> & x, pair<T1, T2>& y);

x – The first pair object.

y – The second pair object whose data is to be exchanged with the first.

Return type

Note The data type of the two pair objects must match to get a successful result.

 #include <iostream>
#include <utility>

using namespace std;

int main( )
pair<string ,string >pr{ “String” , “New string” } , pr1{ “Happy” , “Sad” };


cout< pr.first << ” ” << pr.second ;

pair<char,char> prC{‘G’, ‘O’};

swap(pr1,prC) ; ///error

return 0;


Happy Sad