Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <stdexcept>
- #include <exception>
- #include <vector>
- #include <string>
- #include <cctype>
- #include <iostream>
- #include <forward_list>
- template <class Iterator, class Predicate, class Functor>
- void TransformIf(Iterator first, Iterator last, Predicate p, Functor f) {
- std::vector<std::pair<int, decltype(*first)>> log;
- int cnt = 0;
- for (auto i = first; i != last; ++i) {
- try {
- if (p(*i)) {
- try {
- auto cpy = *i;
- log.emplace_back(cnt, cpy);
- } catch (...) {
- std::cerr << "caught in copy" << std::endl;
- }
- f(*i);
- }
- } catch (...) {
- std::cerr << "caught in predicate/func" << std::endl;
- auto it = first;
- int in = 0;
- for (int j = 0; in < log.size(); ++j) {
- if (log[in].first == j) {
- *it = log[in].second;
- in++;
- }
- ++it;
- }
- throw;
- }
- ++cnt;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement