2012-03-19 2 views
1

이 코드는 작동하지만 문자가 같지 않으면 무언가를 제거하려고하므로 약간 제한적입니다.C++에서 remove_if를 수행하는 방법을 모르겠다.

나는 :: ispunct 대신 :: isalpha를 사용해야한다는 것을 알고 있지만, isalpha와 같지 않으면 제거하는 방법을 이해하지 못한다. 나는이 질문에 고글을 썼지 만, 나는 그들을 이해하지 못했기 때문에 대답을 어디에도 가지 않았다.

textFile[i].erase(remove_if(textFile[i].begin(), textFile[i].end(), ::ispunct), textFile[i].end()); 

아무쪼록 부탁드립니다.

답변

6

나는 컴파일하지 않은,하지만이 작동합니다 : 여기에 관심을

textFile[i].erase(
    remove_if(textFile[i].begin(), textFile[i].end(), std::not1(std::ptr_fun(::isalpha))), 
    textFile[i].end()); 

링크는 다음과 같습니다

표준 펑 didn를하는 경우 충분하지 않아도 구현할 수 있습니다.

struct not_a_character : std::unary_function<char, bool> { 
    bool operator()(char c) const { 
     return !isalpha(c); 
    } 
}; 
로 사용할 수 있습니다

:

textFile[i].erase(
    remove_if(textFile[i].begin(), textFile[i].end(), not_a_character()), 
    textFile[i].end());