Pairs

C++ comes with a large set of ready-made types and functions called the standard library. You have used parts of it already, such as string, vector and cout. This section shows more of it, starting with one of its smallest parts, the pair.

A pair holds two values together, for example a name and an age. The two values can have different types. To use pairs, add #include <utility> at the top.

#include <iostream>
#include <string>
#include <utility>
using namespace std;

int main() {
    pair<string, int> person = {"Alice", 30};
    cout << person.first << "\n";
    cout << person.second << "\n";

    person.second = 31;
    cout << person.first << " is "
         << person.second << "\n";
    return 0;
}

Output:

Alice
30
Alice is 31

pair<string, int> names the two types in angle brackets, just like vector<int> names the type of its values. The first value is called first and the second is called second. They work like the members of a struct, so you can read them and change them.

You can also build a pair with make_pair:

pair<string, int> item = make_pair("pens", 12);
cout << item.first << ": " << item.second << "\n";

Output:

pens: 12

cout cannot print a whole pair in one go. Print first and second one by one, as above.

Returning two values from a function

A function can return only one value. When you need two, return a pair. This function splits a number of minutes into hours and minutes:

#include <iostream>
#include <utility>
using namespace std;

pair<int, int> splitTime(int minutes) {
    return {minutes / 60, minutes % 60};
}

int main() {
    pair<int, int> t = splitTime(135);
    cout << t.first << " h " << t.second
         << " min\n";
    return 0;
}

Output:

2 h 15 min

return {minutes / 60, minutes % 60}; builds the pair from the two values in curly braces. The compiler already knows the return type, so you do not need to write pair<int, int> again.

Giving the two values names

The names first and second do not say what the values mean. C++17 lets you unpack a pair into two new variables in one line. This is called a structured binding:

pair<string, int> person = {"Alice", 30};
auto [name, age] = person;
cout << name << " is " << age << "\n";

Output:

Alice is 30

name gets the first value and age gets the second. You must write auto here, not a type. This also works on the result of a function: auto [hours, mins] = splitTime(135);.

A vector of pairs

A vector of pairs is a simple way to keep a list of records, like names with scores. To sort a vector, use sort from <algorithm>. You will learn how it works in the Algorithms lesson. For now, sort(v.begin(), v.end()) puts the whole vector v in order, smallest first.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    vector<pair<string, int>> scores = {
        {"Emma", 82}, {"Bob", 90},
        {"Alice", 82}, {"Bob", 75}};

    sort(scores.begin(), scores.end());

    for (auto& s : scores) {
        cout << s.first << " " << s.second << "\n";
    }
    return 0;
}

Output:

Alice 82
Bob 75
Bob 90
Emma 82

In the loop, auto saves you from writing the long type pair<string, int>, and the & stops the loop from copying each pair, as in the Vectors and Functions lesson.

Pairs are sorted by first. When two first values are equal, second decides. The two Bobs tie on the name, so Bob 75 comes before Bob 90. If you want to sort by score, put the score first: pair<int, string>.

In the exercise, complete a function that returns the smallest and the largest number of a list as one pair.