Advertisement
kutuzzzov

Урок 6 не очень чистые функции

Jun 27th, 2023
1,264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cassert>
  3. #include <optional>
  4. #include <string_view>
  5. #include <utility>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. // напишите функцию ComputeStatistics, принимающую 5 параметров:
  11. // два итератора, выходное значение для суммы, суммы квадратов и максимального элемента
  12. template <typename InputIt, typename OutSum, typename OutSqSum, typename OutMax>
  13. void ComputeStatistics(InputIt first, InputIt last, OutSum& out_sum, OutSqSum& out_sq_sum, OutMax& out_max) {
  14.     using Elem = std::decay_t<decltype(*first)>;
  15.  
  16.     constexpr bool need_sum = !is_same_v<OutSum, const nullopt_t>;
  17.     constexpr bool need_sq_sum = !is_same_v<OutSqSum, const nullopt_t>;
  18.     constexpr bool need_max = !is_same_v<OutMax, const nullopt_t>;
  19.  
  20.     if (first == last) {
  21.         return;
  22.     }
  23.  
  24.     auto local_sum = Elem{};
  25.     auto local_sqsum = Elem{};
  26.     auto local_max = Elem{};
  27.  
  28.     if constexpr (need_sum) {
  29.         local_sum = *first;
  30.     }
  31.     if constexpr (need_sq_sum) {
  32.         local_sqsum = *first * *first;
  33.     }
  34.     if constexpr (need_max) {
  35.         local_max = *first;
  36.     }
  37.  
  38.     while (first < last - 1) {
  39.         ++first;
  40.         if constexpr (need_sum) {
  41.             local_sum += *first;
  42.         }
  43.         if constexpr (need_sq_sum) {
  44.             local_sqsum += *first * *first;
  45.         }
  46.         if constexpr (need_max) {
  47.             local_max = local_max < *first ? *first : local_max;
  48.         }
  49.     }
  50.     if constexpr (need_sum) {
  51.         out_sum = local_sum;      
  52.     }
  53.     if constexpr (need_sq_sum) {
  54.         out_sq_sum = local_sqsum;
  55.     }
  56.     if constexpr (need_max) {
  57.         out_max = local_max;
  58.     }
  59. }
  60.  
  61. struct OnlySum {
  62.     int value;
  63. };
  64.  
  65. OnlySum operator+(OnlySum l, OnlySum r) {
  66.     return {l.value + r.value};
  67. }
  68. OnlySum& operator+=(OnlySum& l, OnlySum r) {
  69.     return l = l + r;
  70. }
  71.  
  72. int main() {
  73.     vector input = {1, 2, 3, 4, 5, 6};
  74.     int sq_sum;
  75.     std::optional<int> max;
  76.  
  77.     // Переданы выходные параметры разных типов - std::nullopt_t, int и std::optional<int>
  78.     ComputeStatistics(input.begin(), input.end(), nullopt, sq_sum, max);
  79.  
  80.     assert(sq_sum == 91 && max && *max == 6);
  81.  
  82.     vector<OnlySum> only_sum_vector = {{100}, {-100}, {20}};
  83.     OnlySum sum;
  84.  
  85.     // Поданы значения поддерживающие только суммирование, но запрошена только сумма
  86.     ComputeStatistics(only_sum_vector.begin(), only_sum_vector.end(), sum, nullopt, nullopt);
  87.  
  88.     assert(sum.value == 20);
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement