Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.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++ 如何在C+中轻松地将函数应用于集合+;_C++_Templates_Mapreduce - Fatal编程技术网

C++ 如何在C+中轻松地将函数应用于集合+;

C++ 如何在C+中轻松地将函数应用于集合+;,c++,templates,mapreduce,C++,Templates,Mapreduce,我将图像存储为数组,根据其元素类型模板化,如Image或Image等。通常,我需要对这些图像执行操作;例如,我可能需要添加两个图像,或者将一个图像平方(elementwise),等等。所有操作都是元素级的。我想尽可能地写一些东西,比如: float Add(float a, float b) { return a+b; } Image<float> result = Add(img1, img2); 现在,我显然不能完全做到这一点;我写了一些东西,以便我可以调用: Image<

我将图像存储为数组,根据其元素类型模板化,如
Image
Image
等。通常,我需要对这些图像执行操作;例如,我可能需要添加两个图像,或者将一个图像平方(elementwise),等等。所有操作都是元素级的。我想尽可能地写一些东西,比如:

float Add(float a, float b) { return a+b; }
Image<float> result = Add(img1, img2);
现在,我显然不能完全做到这一点;我写了一些东西,以便我可以调用:

Image<float> result = Apply(img1, img2, Add);
图像结果=应用(img1、img2、添加);
但是我似乎无法找到一种通用的方法来检测传递的函数/函数对象的返回类型,因此上面的
complexcom
示例就不适用了;另外,我必须为我想通过的每一个论点写一个新的论点(这似乎是不可避免的)


有没有想过如何实现这一点(尽可能少的样板代码)?

这就是为什么STL中的一元函数、二元函数等类型会产生typedefs。如果您使用std::ptr_fun,您可以使用Apply

e、 g


好吧,你抓住我了,我没有测试。这仍然是可能的,但更多的工作无法直接使用STL的一些功能

#include <iostream>

template<typename T>
struct Image {
  T val_;
  Image(const T& val) : val_(val) {}
};

template<typename Arg1, typename Arg2, typename Result>
struct image_fun_adapter {
  Result (*f)(Arg1, Arg2);
  Image<Result> operator()(const Image<Arg1>& a1, const Image<Arg2>& a2) {
    return Image<Result>(f(a1.val_, a2.val_));
  }
};

template<typename Arg1, typename Arg2, typename Result>
image_fun_adapter<Arg1,Arg2,Result> image_fun_ptr(Result (*f)(Arg1,Arg2)) {
  image_fun_adapter<Arg1,Arg2,Result> rv;
  rv.f = f;
  return rv;
}

float Add(float a, float b) { return a + b; }

int main() {
  Image<float> a(1.0);
  Image<float> b(12.5);
  // this is the cute bit:
  Image<float> c = image_fun_ptr(&Add)(a,b);

  std::cout << c.val_ << std::endl;
  return 0;
}
#包括
模板
结构映像{
T瓦卢;
图像(const T&val):val_(val){}
};
模板
结构映像\u乐趣\u适配器{
结果(*f)(Arg1、Arg2);
图像运算符()(常量图像和a1、常量图像和a2){
返回图像(f(a1.val,a2.val));
}
};
模板
映像_fun_适配器映像_fun_ptr(结果(*f)(Arg1,Arg2)){
图像_fun_适配器rv;
rv.f=f;
返回rv;
}
浮点加法(浮点a,浮点b){返回a+b;}
int main(){
图像a(1.0);
图像b(12.5);
//这是可爱的一点:
图c=图a和图b;

std::cout这就是为什么STL中的一元函数、二元函数等类型具有结果类型typedefs。如果您使用std::ptr\u fun,您可以使用Apply

e、 g


好的,你抓到我了,我没有测试它。这仍然是可能的,但更多的工作不能直接使用STL的一些功能

#include <iostream>

template<typename T>
struct Image {
  T val_;
  Image(const T& val) : val_(val) {}
};

template<typename Arg1, typename Arg2, typename Result>
struct image_fun_adapter {
  Result (*f)(Arg1, Arg2);
  Image<Result> operator()(const Image<Arg1>& a1, const Image<Arg2>& a2) {
    return Image<Result>(f(a1.val_, a2.val_));
  }
};

template<typename Arg1, typename Arg2, typename Result>
image_fun_adapter<Arg1,Arg2,Result> image_fun_ptr(Result (*f)(Arg1,Arg2)) {
  image_fun_adapter<Arg1,Arg2,Result> rv;
  rv.f = f;
  return rv;
}

float Add(float a, float b) { return a + b; }

int main() {
  Image<float> a(1.0);
  Image<float> b(12.5);
  // this is the cute bit:
  Image<float> c = image_fun_ptr(&Add)(a,b);

  std::cout << c.val_ << std::endl;
  return 0;
}
#包括
模板
结构映像{
T瓦卢;
图像(const T&val):val_(val){}
};
模板
结构映像\u乐趣\u适配器{
结果(*f)(Arg1、Arg2);
图像运算符()(常量图像和a1、常量图像和a2){
返回图像(f(a1.val,a2.val));
}
};
模板
映像_fun_适配器映像_fun_ptr(结果(*f)(Arg1,Arg2)){
图像_fun_适配器rv;
rv.f=f;
返回rv;
}
浮点加法(浮点a,浮点b){返回a+b;}
int main(){
图像a(1.0);
图像b(12.5);
//这是可爱的一点:
图c=图a和图b;

std::cout模板专门化

template<typename T>
T Add(const T &left, const T &right)
{
    return left + right;
}

template<typename T>
struct AddTwo
{
    T operator()(const T &left, const T &right)
    {
        return Add(left, right);
    }
};

// something like this (not exactly sure)
template<std::vector<typename V>>
std::vector<V> Add(const std::vector<V> &left, const std::vector<V> &right)
{
    assert(left.size() == right.size());

    std::vector ret(left.size());

    std::transform(left.const_begin(), left.const_end(), right.const_begin(), ret.begin(), AddTwo());

    return ret;
}
模板
T添加(常数T和左、常数T和右)
{
返回左+右;
}
模板
结构AddTwo
{
T运算符()(常数T和左、常数T和右)
{
返回添加(左、右);
}
};
//类似这样的(不完全确定)
模板
std::vector Add(常量std::vector和left,常量std::vector和right)
{
断言(left.size()==right.size());
std::vector ret(left.size());
std::transform(left.const_begin()、left.const_end()、right.const_begin()、ret.begin()、addtwoo());
返回ret;
}

模板专业化

template<typename T>
T Add(const T &left, const T &right)
{
    return left + right;
}

template<typename T>
struct AddTwo
{
    T operator()(const T &left, const T &right)
    {
        return Add(left, right);
    }
};

// something like this (not exactly sure)
template<std::vector<typename V>>
std::vector<V> Add(const std::vector<V> &left, const std::vector<V> &right)
{
    assert(left.size() == right.size());

    std::vector ret(left.size());

    std::transform(left.const_begin(), left.const_end(), right.const_begin(), ret.begin(), AddTwo());

    return ret;
}
模板
T添加(常数T和左、常数T和右)
{
返回左+右;
}
模板
结构AddTwo
{
T运算符()(常数T和左、常数T和右)
{
返回添加(左、右);
}
};
//类似这样的(不完全确定)
模板
std::vector Add(常量std::vector和left,常量std::vector和right)
{
断言(left.size()==right.size());
std::vector ret(left.size());
std::transform(left.const_begin()、left.const_end()、right.const_begin()、ret.begin()、addtwoo());
返回ret;
}
I get(1)错误:预期的类型说明符(1)错误:预期的'>'(2)错误:默认模板参数不能在函数模板中使用(我根据您的示例对行进行了编号)。我使用的是gcc v4.2.4。I get(1)错误:预期的类型说明符(1)错误:预期的'>'(2)错误:默认模板参数不能在函数模板中使用(我根据您的示例对行进行了编号)。我使用的是gcc v4.2.4。
#include <iostream>

template<typename T>
struct Image {
  T val_;
  Image(const T& val) : val_(val) {}
};

template<typename Arg1, typename Arg2, typename Result>
struct image_fun_adapter {
  Result (*f)(Arg1, Arg2);
  Image<Result> operator()(const Image<Arg1>& a1, const Image<Arg2>& a2) {
    return Image<Result>(f(a1.val_, a2.val_));
  }
};

template<typename Arg1, typename Arg2, typename Result>
image_fun_adapter<Arg1,Arg2,Result> image_fun_ptr(Result (*f)(Arg1,Arg2)) {
  image_fun_adapter<Arg1,Arg2,Result> rv;
  rv.f = f;
  return rv;
}

float Add(float a, float b) { return a + b; }

int main() {
  Image<float> a(1.0);
  Image<float> b(12.5);
  // this is the cute bit:
  Image<float> c = image_fun_ptr(&Add)(a,b);

  std::cout << c.val_ << std::endl;
  return 0;
}
template<typename T>
T Add(const T &left, const T &right)
{
    return left + right;
}

template<typename T>
struct AddTwo
{
    T operator()(const T &left, const T &right)
    {
        return Add(left, right);
    }
};

// something like this (not exactly sure)
template<std::vector<typename V>>
std::vector<V> Add(const std::vector<V> &left, const std::vector<V> &right)
{
    assert(left.size() == right.size());

    std::vector ret(left.size());

    std::transform(left.const_begin(), left.const_end(), right.const_begin(), ret.begin(), AddTwo());

    return ret;
}