Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <cassert>
- #include <optional>
- #include <string_view>
- #include <utility>
- #include <vector>
- using namespace std;
- // напишите функцию ComputeStatistics, принимающую 5 параметров:
- // два итератора, выходное значение для суммы, суммы квадратов и максимального элемента
- template <typename InputIt, typename OutSum, typename OutSqSum, typename OutMax>
- void ComputeStatistics(InputIt first, InputIt last, OutSum& out_sum, OutSqSum& out_sq_sum, OutMax& out_max) {
- using Elem = std::decay_t<decltype(*first)>; // определить тип переданного параметра чтобы создать временную переменную
- Elem temp_sum{ *first };
- Elem temp_sq_sum{ *first };
- Elem temp_max{ *first };
- for (auto it = first + 1; it != last; it++) {
- auto value = *it;
- if constexpr (!is_same_v<OutSum, const nullopt_t>) {
- temp_sum += value;
- }
- if constexpr (!is_same_v<OutSqSum, const nullopt_t>) {
- temp_sq_sum += value * value;
- }
- if constexpr (!is_same_v<OutMax, const nullopt_t>) {
- if (temp_max < value) {
- temp_max = value;
- }
- }
- }
- if constexpr (!is_same_v<OutSum, const nullopt_t>) {
- out_sum = temp_sum;
- }
- if constexpr (!is_same_v<OutSqSum, const nullopt_t>) {
- out_sq_sum = temp_sq_sum;
- }
- if constexpr (!is_same_v<OutMax, const nullopt_t>) {
- out_max = temp_max;
- }
- }
- struct OnlySum {
- int value;
- };
- OnlySum operator+(OnlySum l, OnlySum r) {
- return {l.value + r.value};
- }
- OnlySum& operator+=(OnlySum& l, OnlySum r) {
- return l = l + r;
- }
- int main() {
- vector input = {1, 2, 3, 4, 5, 6};
- int sq_sum;
- std::optional<int> max;
- // Переданы выходные параметры разных типов - std::nullopt_t, int и std::optional<int>
- ComputeStatistics(input.begin(), input.end(), nullopt, sq_sum, max);
- assert(sq_sum == 91 && max && *max == 6);
- vector<OnlySum> only_sum_vector = {{100}, {-100}, {20}};
- OnlySum sum;
- // Поданы значения поддерживающие только суммирование, но запрошена только сумма
- ComputeStatistics(only_sum_vector.begin(), only_sum_vector.end(), sum, nullopt, nullopt);
- assert(sum.value == 20);
- }
Advertisement
Add Comment
Please, Sign In to add comment