简体   繁体   English

定义模板参数的迭代器类型

[英]Define Iterator type of template argument

In function below, how to tell the compiler that Iterator is the iterator of Cont ?在下面的函数中,如何告诉编译器 Iterator 是 Cont 的迭代器?

template<typename Cont, typename Pred>
Iterator<Cont> find_if(const Cont &c, Pred p)
{
    return std::find_if(std::begin(c), std::end(c), p);
}
template<typename Cont, typename Pred>
auto find_if(const Cont &c, Pred p)
-> decltype(std::begin(c)) // HERE
{
    return std::find_if(std::begin(c), std::end(c), p);
}

Or since version C++14:或者从 C++14 版开始:

template<typename Cont, typename Pred>
auto find_if(const Cont &c, Pred p) //All you need is auto
{
    return std::find_if(std::begin(c), std::end(c), p);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM