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); |
---|
Parameters:
x – The first pair object.
y – The second pair object whose data is to be exchanged with the first.
Return type
void
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” }; swap(pr,pr1); cout< pr.first << ” ” << pr.second ; pair<char,char> prC{‘G’, ‘O’}; swap(pr1,prC) ; ///error return 0; }
Output
Happy Sad