CJCoding With Joseph
15free / daySign inCreate account

Swap Two Variables with References

Write a function template swapValues(T& first, T& second) that exchanges the two values using a temporary variable. Do not call std::swap.

The parameters are REFERENCES, so the function changes the caller's own variables. If they were passed by value, the function would swap two private copies and the originals would stay the same. The tests swap two int variables holding 1 and 2 and print them separated by a space, then swap two std::string variables holding left and right and print them the same way.

Do NOT write a main function.

Expected Output:

2 1
right left

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Templates
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.