-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExer10_32.cpp
47 lines (47 loc) · 1.62 KB
/
Exer10_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
44
45
46
47
// Note: this file uses a changed version of Sales_item.h, don't use it anywhere else.
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include "Sales_item_Exer10_32.h"
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::istream_iterator;
using std::ostream_iterator;
int main()
{
istream_iterator<Sales_item> is_iter(cin), eof;
ostream_iterator<Sales_item> os_iter(cout, "\n");
vector<Sales_item> vec_item(is_iter, eof);
vector<Sales_item> vec_sum;
sort(vec_item.begin(), vec_item.end(), compareIsbn);
auto beg = vec_item.begin();
while(beg != vec_item.end())
{
auto same_item = beg, trans_item = beg;
while(trans_item != vec_item.end())
{
same_item = trans_item;
trans_item = find(++trans_item, vec_item.end(), *beg);
}
vec_sum.push_back(accumulate(beg, ++same_item, Sales_item(beg->isbn())));
beg = same_item;
}
cout << "Sorted items: " << endl;
for(const auto &si : vec_item)
os_iter = si;
cout << "Sum of each book: " << endl;
for(const auto &si : vec_sum)
os_iter = si;
return 0;
}
// ******First Modification******
// compareIsbn(), see header Sales_item.h, but == is substituted with <.
// substituting == because we want to really sort by isbn.
// ******Second Modification******
// Overloading operator ==, see header Sales_item.h, line 86 and 87 are commented.
// Changing the implementation of overloading operator == because the exercise
// requires us to use find. If we use find_if rather than find, we needn't
// change the implementation.