2009-04-20 3 views
2

는 여기 바보 야하지만 문자열의 반복 처리를하는 경우, find_if 것 술어에 대한 함수 서명을받을 수 없습니다 :이 경우사용하여 표준 : : find_if : 문자열

bool func(char); 

std::string str; 
std::find_if(str.begin(), str.end(), func)) 

을 구글은 실제로 END_LINK하면 관심있는 당신은 아마 std::find()보다는 std::find_if() 사용할 수있는 표준 : : 문자열 str 내에서 단일 문자 c을 찾기 위해 노력하는 경우

+0

무엇이 문제입니까? '여기 누구 있어요? '? – kim366

답변

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

bool func(char c) { 
    return c == 'x'; 
} 

int main() { 
    std::string str ="abcxyz";; 
    std::string::iterator it = std::find_if(str.begin(), str.end(), func); 
    if (it != str.end()) { 
     std::cout << "found\n"; 
    } 
    else { 
     std::cout << "not found\n"; 
    } 
} 
+0

그래, 어리 석다는 것을 알았어. 나는 if 문에서 find_if를 가지고 있었고 오류 메시지를 해독 할 수 없었다. 덕분에 – Patrick

4

? 내 친구 :(여기 사람이되어 있었다. 그리고되지 않았습니다을 사용하는 것이 더 나을 것입니다.의 멤버 함수 string::find() 대신 <algorithm>의 함수.

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

int main() 
{ 
    std::string str = "abcxyz"; 
    size_t n = str.find('c'); 
    if(std::npos == n) 
    cout << "Not found."; 
    else 
    cout << "Found at position " << n; 
    return 0; 
} 
+1

고마워.하지만 내가 뭘 하려는지는 모르겠다. 나는 isNumeric 함수를 리팩토링하고 있었다. – Patrick

관련 문제