Operator < for Structs

Lab C++ Intermediate 20 points

Give struct Version { int major, minor, patch; } an operator< using std::tie. Read versions like 1.10.2, sort them (no comparator needed for a struct), and print them.

Input

n, then n versions.

Output

n lines.

Example

Input

4
1.10.0
1.9.3
1.2.0
2.0.0

Output

1.2.0
1.9.3
1.10.0
2.0.0
Sample input available Sample output available
Editor
C++

Hints

std::tie(a, b, c) builds a tuple of references that compares element by element.

Solution explanation

Comparing versions as numbers (not text) is why 1.10 sorts after 1.9.

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

struct Version {
    int major, minor, patch;
    bool operator<(const Version &o) const {
        return tie(major, minor, patch) < tie(o.major, o.minor, o.patch);
    }
};

int main() {
    int n;
    cin >> n;
    vector<Version> vs(n);
    for (auto &v : vs) {
        string s;
        cin >> s;
        sscanf(s.c_str(), "%d.%d.%d", &v.major, &v.minor, &v.patch);
    }
    sort(vs.begin(), vs.end());
    for (const auto &v : vs) cout << v.major << "." << v.minor << "." << v.patch << "\n";
    return 0;
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD