Skip to content

Commit 436dc4d

Browse files
committed
refactor the calculateTimeElapsed to use minmax_element
1 parent 1969273 commit 436dc4d

File tree

1 file changed

+4
-13
lines changed

1 file changed

+4
-13
lines changed

rest/service/PricePlanService.h

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <domain/PricePlan.h>
77
#include <service/MeterReadingService.h>
88

9+
#include <algorithm>
910
#include <ctime>
1011
#include <map>
1112
#include <optional>
@@ -38,20 +39,10 @@ class PricePlanService {
3839
MeterReadingService &meterReadingService_;
3940

4041
static auto calculateTimeElapsed(const std::vector<ElectricityReading> &electricityReadings) {
41-
ElectricityReading first = *electricityReadings.begin();
42-
ElectricityReading last = *electricityReadings.begin();
43-
for (auto it = electricityReadings.begin(); it != electricityReadings.end(); it++) {
44-
if (it->getTime() < first.getTime()) {
45-
first = *it;
46-
}
47-
if (it->getTime() > first.getTime()) {
48-
last = *it;
49-
}
50-
}
51-
52-
std::chrono::duration duration = last.getTime() - first.getTime();
42+
const auto [min, max] = std::minmax_element(std::begin(electricityReadings), std::end(electricityReadings),
43+
[](auto const &l, auto const &r) { return l.getTime() < r.getTime(); });
5344

54-
return duration;
45+
return max->getTime() - min->getTime();
5546
}
5647

5748
static int calculateCost(const std::vector<ElectricityReading> &electricityReadings, const PricePlan &pricePlan) {

0 commit comments

Comments
 (0)