CJCoding With Joseph
15free / daySign inCreate account

Flip a Two-Type Pair

Complete the class template Pair<T, U>, which stores a first of type T and a second of type U:

- Pair(T a, U b): stores the two values - void print() const: prints first, then / (space, slash, space), then second, then a newline - Pair<U, T> flipped() const: returns a NEW pair whose first value is this pair's second and whose second value is this pair's first

Notice the return type: swapping the values also swaps the type arguments, so flipping a Pair<int, std::string> gives a Pair<std::string, int>, a different class made from the same template. The tests print a Pair<int, std::string> holding 1 and one, print its flipped copy, and print the flipped copy of a Pair<char, double> holding x and 2.5, one line each.

Do NOT write a main function.

Expected Output:

1 / one
one / 1
2.5 / x

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

Hints3 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.