Comparison for remove_if in c ++ does not accept char

0

Hello, I'm trying to use a

  

remove_if

In c ++ however when it arrives at the input part I can not determine for it to detect if the string I'm pointing has a char. Here is my code:

temp.erase(remove_if(temp.begin(), temp.end(), "("), temp.end());

temp is a string.

    

asked by anonymous 14.12.2017 / 14:58

1 answer

2

The remove_if gets 3 parameters, the start and end iterators, and a function that takes a parameter and returns a bool . In this case you can pass the function using its name as if it were a variable.

Follow the code below:

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

bool isParenthesis (char c) {
  return c == '(';
}

int main () {
  string temp = "123(456";

  temp.erase(remove_if(temp.begin(), temp.end(), isParenthesis));

  cout << temp;
  cout << endl;
  return 0;
}

You can also see an example in documentation .

    
14.12.2017 / 15:21