Search code examples
c++stlstd-pairgarbageutility-method

Why this code outputs garbage value after swapping the values?


Take two numbers as input and swap them and print the swapped values.You do not need to print anything, it has already been taken care of. Just implement the given function.And the functions looks like this.

 pair < int, int > swap(pair < int, int > swapValues) {
         int c;
         c=swapValues.first; 
         swapValues.first=swapValues.second;
         swapValues.second=c;
         cout<<swapValues.first<<" "<<swapValues.second<<"\n";
    
         }

Solution

  • Just replace the line cout<<swapValues.first<<" "<<swapValues.second<<"\n"; with return swapValues;

    As stated in your question

    You do not need to print anything, it has already been taken care of.