Template with Two Types

Lab C++ Intermediate 20 points

Write template <typename K, typename V> struct Entry with key, value and a print() method. Read name/score entries into a vector of Entry<string, int> and print them with the highest score first (use sort with a lambda).

Input

n, then n lines name score.

Output

n lines name=score.

Example

Input

4
ann 50
bob 90
cy 50
dan 70

Output

bob=90
dan=70
ann=50
cy=50
Sample input available Sample output available
Editor
C++

Hints

Generic lambdas (auto parameters) work with any type.

Solution explanation

Multiple template parameters let one definition cover many type combinations.

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

template <typename K, typename V>
struct Entry {
    K key;
    V value;
    void print() const { cout << key << "=" << value << "\n"; }
};

int main() {
    int n;
    cin >> n;
    vector<Entry<string, int>> entries(n);
    for (auto &e : entries) cin >> e.key >> e.value;
    sort(entries.begin(), entries.end(), [](const auto &a, const auto &b) {
        if (a.value != b.value) return a.value > b.value;
        return a.key < b.key;
    });
    for (const auto &e : entries) e.print();
    return 0;
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD