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