merge_adjacent
joboc
3,670 views
Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
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
#include <iostream>
#include <vector>
/////////// ALGORITHM TO IMPLEMENT ////////////////////
template <typename ForwardIterator, typename OutputIterator, typename Equal, typename Merge>
void merge_adjacent(ForwardIterator first, ForwardIterator last, OutputIterator out, Equal equal, Merge merge)
{
// Write your code here, and make the tests pass!
}
////////////////////////////////////////////////////////
/////////// SALE CLASS /////////////////////////////////
class Sale
{
public:
Sale(int day, double amount) : day_(day), amount_(amount) {}
int getDate() const { return day_; }
double getAmount() const { return amount_; }
private:
int day_;
double amount_;
};
bool operator==(Sale const& sale1, Sale const& sale2)
{
return sale1.getDate() == sale2.getDate() && sale1.getAmount() == sale2.getAmount();
}
bool sameDate(Sale const& sale1, Sale const& sale2)
{
return sale1.getDate() == sale2.getDate();
}
Sale mergeSales(Sale const& sale1, Sale const& sale2)
{
if (sale1.getDate() != sale2.getDate()) throw "Error: cannot add sales with different dates";
return Sale(sale1.getDate(), sale1.getAmount() + sale2.getAmount());
}
////////////////////////////////////////////////////////
/////////// TEST CODE: MAKE IT PASS! ///////////////////
Enter to Rename, Shift+Enter to Preview
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content