Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 传递std::max\u element()或std::min\u element()作为参数_C++_C++11 - Fatal编程技术网

C++ 传递std::max\u element()或std::min\u element()作为参数

C++ 传递std::max\u element()或std::min\u element()作为参数,c++,c++11,C++,C++11,有没有办法将std::max\u element()或std::min\u element()传递给模板类方法?例如: #include <vector> #include <algorithms> template <typename T> class A { public: int fooMax() { return foo(std::max_element()); } int fooMin()

有没有办法将std::max\u element()或std::min\u element()传递给模板类方法?例如:

#include <vector>
#include <algorithms>

template <typename T>
class A 
{
public: 
    int fooMax()
    {
        return foo(std::max_element());
    }

    int fooMin()
    {
        return foo(std::min_element());
    }

private:
    std::vector<T> data_;

private:
    int foo(??? func)
    {
        auto it = func(data_.begin(), data_.end());

        int i = do_something();
        return i;
    }
};
#包括
#包括
模板
甲级
{
公众:
int fooMax()
{
返回foo(std::max_element());
}
int fooMin()
{
返回foo(std::min_元素());
}
私人:
std::矢量数据;
私人:
int foo(?func)
{
auto it=func(数据开始(),数据结束());
int i=做某事();
返回i;
}
};

我尝试使用函数指针,但它没有编译。

您可以使用lambda表达式:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

template <typename T>
struct A {
   vector<T> v;
   A(vector<T> v): v(v) { }
   template <class Lambda>
   T element(Lambda &&lambda) {
      return *lambda(v.begin(), v.end());
   }
};

int main() {
   vector<int> vi {1, 2, 3, 4, 5, 6};
   A<int> a(vi);
   cout << a.element([](typename vector<int>::iterator begin,
                        typename vector<int>::iterator end){ 
                            return max_element(begin, end); 
                        }) << endl;
}
#包括
#包括
#包括
使用名称空间std;
模板
结构A{
向量v;
A(向量v):v(v){}
模板
T元素(λ和λ){
return*lambda(v.begin(),v.end());
}
};
int main(){
向量vi{1,2,3,4,5,6};
A(vi);

cout您可以使用lambda表达式:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

template <typename T>
struct A {
   vector<T> v;
   A(vector<T> v): v(v) { }
   template <class Lambda>
   T element(Lambda &&lambda) {
      return *lambda(v.begin(), v.end());
   }
};

int main() {
   vector<int> vi {1, 2, 3, 4, 5, 6};
   A<int> a(vi);
   cout << a.element([](typename vector<int>::iterator begin,
                        typename vector<int>::iterator end){ 
                            return max_element(begin, end); 
                        }) << endl;
}
#包括
#包括
#包括
使用名称空间std;
模板
结构A{
向量v;
A(向量v):v(v){}
模板
T元素(λ和λ){
return*lambda(v.begin(),v.end());
}
};
int main(){
向量vi{1,2,3,4,5,6};
A(vi);

cout
std::max_元素
std::min_元素
是模板函数,您需要实例化它们:

template <typename T>
class A
{
public:
    int fooMax()
    {
        return foo(&std::max_element<typename std::vector<T>::const_iterator>);
    }

    int fooMin()
    {
        return foo(&std::min_element<typename std::vector<T>::const_iterator>);
    }

private:
    std::vector<T> data_;

private:
    template<typename func_type>
    int foo(func_type func)
    {
        auto it=func(data_.begin(), data_.end());

        return 0;
    }
};
模板
甲级
{
公众:
int fooMax()
{
返回foo(&std::max_元素);
}
int fooMin()
{
返回foo(&std::min_元素);
}
私人:
std::矢量数据;
私人:
模板
int foo(func_类型func)
{
auto it=func(数据开始(),数据结束());
返回0;
}
};
使用带有
-std=c++14的gcc 5.3.1进行测试


如果您选择,也可以选择实例化
std::vector::iterator

std::max_元素
std::min_元素
都是模板函数,您需要实例化它们:

template <typename T>
class A
{
public:
    int fooMax()
    {
        return foo(&std::max_element<typename std::vector<T>::const_iterator>);
    }

    int fooMin()
    {
        return foo(&std::min_element<typename std::vector<T>::const_iterator>);
    }

private:
    std::vector<T> data_;

private:
    template<typename func_type>
    int foo(func_type func)
    {
        auto it=func(data_.begin(), data_.end());

        return 0;
    }
};
模板
甲级
{
公众:
int fooMax()
{
返回foo(&std::max_元素);
}
int fooMin()
{
返回foo(&std::min_元素);
}
私人:
std::矢量数据;
私人:
模板
int foo(func_类型func)
{
auto it=func(数据开始(),数据结束());
返回0;
}
};
使用带有
-std=c++14的gcc 5.3.1进行测试


如果您选择的话,您也可以选择实例化一个
std::vector::iterator

我知道这不是您想要看到的。但是如果您想将它作为函数指针传递

#include <vector>
#include <algorithm>

int do_something();

template <typename T>
class A 
{
public: 
    int fooMax()
    {
        return foo(std::max_element<const_iterator>);
    }

    int fooMin()
    {
        return foo(std::min_element<const_iterator>);
    }

private:
    std::vector<T> data_;

private:
    using const_iterator = typename std::vector<T>::const_iterator;
  using fp = const_iterator (*)(const_iterator, const_iterator);


    int foo(fp func)
    {
        auto it = func(data_.begin(), data_.end());

        int i = do_something();
        return i;
    }
};
#包括
#包括
int do_something();
模板
甲级
{
公众:
int fooMax()
{
返回foo(标准::max_元素);
}
int fooMin()
{
返回foo(标准::最小元素);
}
私人:
std::矢量数据;
私人:
使用const_迭代器=typename std::vector::const_迭代器;
使用fp=const_迭代器(*)(const_迭代器,const_迭代器);
int foo(fp func)
{
auto it=func(数据开始(),数据结束());
int i=做某事();
返回i;
}
};

我知道这不是您想要看到的。但是如果您想将其作为函数指针传递

#include <vector>
#include <algorithm>

int do_something();

template <typename T>
class A 
{
public: 
    int fooMax()
    {
        return foo(std::max_element<const_iterator>);
    }

    int fooMin()
    {
        return foo(std::min_element<const_iterator>);
    }

private:
    std::vector<T> data_;

private:
    using const_iterator = typename std::vector<T>::const_iterator;
  using fp = const_iterator (*)(const_iterator, const_iterator);


    int foo(fp func)
    {
        auto it = func(data_.begin(), data_.end());

        int i = do_something();
        return i;
    }
};
#包括
#包括
int do_something();
模板
甲级
{
公众:
int fooMax()
{
返回foo(标准::max_元素);
}
int fooMin()
{
返回foo(标准::最小元素);
}
私人:
std::矢量数据;
私人:
使用const_迭代器=typename std::vector::const_迭代器;
使用fp=const_迭代器(*)(const_迭代器,const_迭代器);
int foo(fp func)
{
auto it=func(数据开始(),数据结束());
int i=做某事();
返回i;
}
};

如果要传递函数指针,必须手动实例化模板。但这可能会产生间接成本并阻止内联,因此更好的解决方案是传递一个函子(可能是您必须自己定义的函子)并且将
foo
也作为模板。如果要传递函数指针,则必须手动实例化模板。但这可能会产生间接成本并阻止内联,因此更好的解决方案是传递一个函子(可能是您必须自己定义的函子)并将
foo
也作为模板。