Advertisement
anechka_ne_plach

Untitled

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