这是我的代码片段:
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
bool next(int j)
{
return(j<3);
}
int main()
{
......
cin>>m;
int h[m];
memset(h, 0, sizeof(h));
.......
int *p;
p = find_if(h, h+m, next);
......
}
Run Code Online (Sandbox Code Playgroud)
编译时我收到以下错误:
没有用于调用'find_if(int*,int*,)'的匹配函数
template _IIter std :: find_if(_IIter,_IIter,_Predicate)
模板参数扣除/替换失败:
无法推断模板参数'_Predicate'
您是C++有点神秘的查找规则的受害者!
因为你没有资格next,并且因为你写了using namespace std,并且因为std::next存在,所以编译器会考虑std::next候选者(即使查找失败了),也没有别的!
鉴定您的功能:
find_if(h, h+m, ::next);
// ^^
Run Code Online (Sandbox Code Playgroud)
代表您的代码的测试用例:
#include <algorithm>
using namespace std;
bool next(int j)
{
return (j<3);
}
int main()
{
int h[5] = {};
const size_t m = 2;
find_if(h, h+m, next);
}
Run Code Online (Sandbox Code Playgroud)
并修复:
#include <algorithm>
using namespace std;
bool next(int j)
{
return (j<3);
}
int main()
{
int h[5] = {};
const size_t m = 2;
find_if(h, h+m, ::next);
}
Run Code Online (Sandbox Code Playgroud)