下面的代码中,报错:error C2512: “_mybind1st,int>”: 没有合适的默认构造函数可用,不知道这样定义_mynot1正确不?
vector::Iterator it1 = myfind_if(vec1.begin(),
vec1.end(), mybind1st(mygreater(), 50)); //意思是50>(容器中的某一个值)
vector::Iterator it2 = myfind_if(vec1.begin(),
vec1.end(), mynot1(mybind1st(myless(), 50))); //意思是(50<(容器中的某一个值))取反即可得到
template
Iterator myfind_if(Iterator first, Iterator last, Compare &comp)
{
for (; first != last; ++first)
{
if (comp(*first)) // comp.operator()(*first) 一元函数对象 val隐藏,然后再和其中的每一个元素进行比较
return first;
}
return last;
}
//类模板
template
class _mybind1st
{
public:
typedef T vaule_type;
_mybind1st(Compare &comp, const T &val)
{
m_comp = comp;
m_val = val;
}
bool operator()(const T &val)
{
return m_comp( m_val, val);
}
private:
Compare m_comp;
T m_val;
};
//函数模板
template
_mybind1st mybind1st(Compare &comp, const T &val)
{
return _mybind1st(comp, val);
}
//类模板
template
class _mynot1
{
public:
//typedef typename Compare::value_type T
_mynot1(Compare &comp)
{
m_comp = comp;
}
template<typename T>
bool operator()(const T &val)
{
return !m_comp(val);
}
private:
Compare &m_comp;
};
template
_mynot1 mynot1(Compare &comp)
{
return _mynot1(comp);
}