Tuples

Lab C++ Intermediate 20 points

Read people name age city into a vector<tuple<string, int, string>>. Sort them by city then age (tuples compare element by element, so build sort keys with tie in a lambda) and print them.

Input

n, then n lines.

Output

n lines.

Example

Input

4
ann 30 pune
bob 25 delhi
cy 22 pune
dan 40 delhi

Output

bob 25 delhi
dan 40 delhi
cy 22 pune
ann 30 pune
Sample input available Sample output available
Editor
C++

Hints

get<2>(t) reads the third element.

tie builds a tuple of references for comparison.

Solution explanation

Tuples are handy for quick records; structured bindings unpack them.

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

int main() {
    int n;
    cin >> n;
    vector<tuple<string, int, string>> people;
    for (int i = 0; i < n; i++) {
        string name, city;
        int age;
        cin >> name >> age >> city;
        people.emplace_back(name, age, city);
    }
    sort(people.begin(), people.end(), [](const auto &a, const auto &b) {
        return tie(get<2>(a), get<1>(a)) < tie(get<2>(b), get<1>(b));
    });
    for (const auto &[name, age, city] : people) cout << name << " " << age << " " << city << "\n";
    return 0;
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD