1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
/*
Challenge 245
Task 1: Sort Language
Submitted by: Mohammad S Anwar
You are given two array of languages and its popularity.
Write a script to sort the language based on popularity.
Example 1
Input: @lang = ('perl', 'c', 'python')
@popularity = (2, 1, 3)
Output: ('c', 'perl', 'python')
Example 2
Input: @lang = ('c++', 'haskell', 'java')
@popularity = (1, 3, 2)
Output: ('c++', 'java', 'haskell')
*/
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Elem {
string name;
int pop;
Elem(const string& name_ = "", int pop_ = 0) :name(name_), pop(pop_) {}
bool operator<(const Elem& other) { return pop < other.pop ? true : false; }
};
int main(int argc, char* argv[]) {
if (argc < 2) {
cerr << "Usage: ch-1 lang pop lang pop ..." << endl;
exit(EXIT_FAILURE);
}
vector<Elem> langs;
for (int i = 1; i + 1 < argc; i += 2) {
langs.emplace_back(argv[i], atoi(argv[i + 1]));
}
sort(langs.begin(), langs.end());
for (auto& lang : langs)
cout << lang.name << " ";
cout << endl;
}
|