-
Notifications
You must be signed in to change notification settings - Fork 15
/
Exer11_32.cpp
43 lines (43 loc) · 1.22 KB
/
Exer11_32.cpp
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <utility>
#include <algorithm>
using std::cout;
using std::cin;
using std::endl;
using std::ifstream;
using std::string;
using std::vector;
using std::multimap;
using std::pair;
int main(int argc, char *argv[])
{
if(argc != 2 && argc != 1)
return -1;
ifstream is(argv[1]);
multimap<string, string> authors;
string aut, works;
while(getline(is, aut))
{
getline(is, works);
authors.insert({aut, works});
}
auto beg = authors.begin();
while(beg != authors.end())
{
// to sort works by alphabetically, we have to find out a way to sort works
// either use ordered map or sequential container and generic algorithm
auto author_works = authors.equal_range(beg->first);
vector<pair<string, string>> vec(author_works.first, author_works.second);
sort(vec.begin(), vec.end(),
[](const pair<string, string> &a1, const pair<string, string> &a2) { return a1.second < a2.second; });
for(const auto &p : vec)
cout << p.first << ": " << p.second << "\n";
beg = author_works.second;
}
cout << endl;
return 0;
}