remove.cpp

(plain text)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <algorithm>
#include <iostream>
using namespace std;

struct decurly {
  struct booly { booly () : b(false) { cout << "booly()" << endl; } ~booly () { cout << "~booly()" << endl; } bool b; };

  decurly (const booly &b = booly()) : triggered(const_cast<bool &>(b.b)) {
    cout << "decurly()" << endl;
  }

  ~decurly () {
    cout << "~decurly()" << endl;
  }

  bool operator() (char ch) {
    if (ch == '{') { triggered = true; }
    else if (ch == '}') { triggered = false; }
    return triggered || ch == '}';
  }

  bool &triggered;
};

bool func (char ch) {
  static bool triggered = false;
  if (ch == '{') { triggered = true; }
  else if (ch == '}') { triggered = false; }
  return triggered || ch == '}';
}

int main () {
  char array[] = "out { and in } and out";
//  *remove_if(array, array + strlen(array), func) = '\0';
  *remove_if(array, array + strlen(array), decurly()) = '\0';
  cout << array << endl;
}

/*****
output:
booly()
decurly()
~decurly()
~decurly()
~decurly()
~decurly()
~decurly()
~decurly()
~decurly()
~decurly()
~booly()
out  and out
*****/