TL ; DR:
A pair keeps two values together, like a movie's title and year. Read them with .first and .second, or unpack them into variables.
pair<string, int> movie = {"Frozen", 2013}; // a title and a year
cout << movie.first << endl; // Frozen
cout << movie.second << endl; // 2013
auto [title, year] = movie; // unpacking
cout << title << " " << year << endl; // Frozen 2013
Full lesson:
Some values belong together. A movie has a title and a year. A point has an x and a y. C++ keeps two values like these in a pair.
pair<string, int>is a pair of astringand anint. The types go in the angle brackets, in order.pair<string, int> movie = {"Frozen", 2013};makes one. The values go in curly braces.movie.firstis the first value,"Frozen".movie.secondis the second,2013.
auto [title, year] = movie; unpacks the pair into two new variables. You have seen this before, in for (auto& [name, points] : scores).
A function can return a pair. Its return type is the pair's type, like pair<int, int>. Then return {a, b}; builds it.
cout << movie doesn't compile. Print movie.first and movie.second instead.
Your task
Rex the dog went for a long walk. pet holds his name and his age.
First, make splitTime return its pair: the whole hours and the minutes left.
Then unpack pet into name and age.
Last, call splitTime(135). Unpack its result into hours and mins.
The code already prints the three lines.
Expected output:
Rex
Rex is 4
Rex walked 2 h 15 minFree plan: 30 tutor messages. PRO is unlimited.
