Advertisement
anechka_ne_plach

Untitled

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