나는 람다 함수와 함께 ptr_fun을 사용하는 다음 프로그램을 가지고있다.람다 함수가있는 ptr_fun
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
int main()
{
string target="aa";
vector<string> v1;
v1.push_back("aa");
v1.push_back("bb");
auto stringcasecmp=[](string lhs, string rhs)->int
{
return strcasecmp(lhs.c_str(), rhs.c_str());
};
auto pos = find_if(
v1.begin(), v1.end(),
not1(bind2nd(ptr_fun(stringcasecmp), target))
);
if (pos != v1.end())
cout << "The search for `" << target << "' was successful.\n"
"The next string is: `" << pos[1] << "'.\n";
}
다음과 같은 오류 메시지가 나타납니다.
stackoverflow.cpp: In function ‘int main()’:
stackoverflow.cpp:21:41: error: no matching function for call to ‘ptr_fun(main()::<lambda(std::string, std::string)>&)’
stackoverflow.cpp:22:6: error: unable to deduce ‘auto’ from ‘<expression error>’
컴파일 할 때 (최소한으로) 코드를 어떻게 수정합니까?
그냥 조금 (!) 참고 : 당신이 하나처럼 람다를 사용하여 계획하는 경우가 여기 많이, 당신은 수도 인수를 참조로 전달하는 것을 고려하십시오. 많은 문자열 복사를 저장하십시오. –
'stringcasecmp' 람다가 재귀적임을 알 수 있습니다! – quamrana
@quamrana 왜 재귀입니까? – ggg