Advertisement
anechka_ne_plach

stay safe

Oct 14th, 2021
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #pragma once
  2. #include <map>
  3. #include <stdexcept>
  4. #include <exception>
  5.  
  6. template <class Iterator, class Predicate, class Functor>
  7. void TransformIf(Iterator first, Iterator last, Predicate p, Functor f) {
  8.     std::map<int, decltype(*first)> prev_state;
  9.     int cnt = 0;
  10.     for (auto i = first; i != last; ++i) {
  11.         try {
  12.             if (p(*i)) {
  13.                 try {
  14.                     log.emplace_back(cnt, *i);
  15.                 } catch (std::exception& e) { //catching copy
  16.                     try {
  17.                         f(*i);
  18.                     } catch (std::exception &u) { //catching f
  19.                         throw u;
  20.                     }
  21.                 }
  22.             }
  23.             cnt++;
  24.         } catch(std::exception& e) { //catching p
  25.             auto it = first;
  26.             int in = 0;
  27.             for (int j = 0; j != cnt && in < log.size(); ++j) {
  28.                 if (log[in].first == j) {
  29.                     try {
  30.                         *it = log[in].second;
  31.                     } catch(std::exception& u) { //catching copy
  32.                         throw u;
  33.                     }
  34.                 }
  35.                 ++it;
  36.             }
  37.             throw e;
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement