forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex10_32.cpp
39 lines (36 loc) · 1.18 KB
/
ex10_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
//
// ex10_32.cpp
// Exercise 10.32
//
// Created by pezy on 12/13/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// Rewrite the bookstore problem from 1.6 (p. 24) using a vector to hold the
// transactions
// and various algorithms to do the processing.
// Use sort with your compareIsbn function from 10.3.1 (p. 387) to arrange the
// transactions in order,
// and then use find and accumulate to do the sum.
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <numeric>
#include "../include/Sales_item.h"
int main()
{
std::istream_iterator<Sales_item> in_iter(std::cin), in_eof;
std::vector<Sales_item> vec;
while (in_iter != in_eof) vec.push_back(*in_iter++);
sort(vec.begin(), vec.end(),
[](Sales_item const& lhs, Sales_item const& rhs) {
return lhs.isbn() < rhs.isbn();
});
for (auto beg = vec.cbegin(), end = beg; beg != vec.cend(); beg = end) {
end = find_if(beg, vec.cend(), [beg](const Sales_item& item) {
return item.isbn() != beg->isbn();
});
std::cout << std::accumulate(beg, end, Sales_item(beg->isbn()))
<< std::endl;
}
}