简体   繁体   English

使用模板在C ++中映射函数

[英]Map function in C++ with templates

I'm trying to learn templates in C++ and one of the things I was trying was to code a map function like the ones you typically find in functional languages. 我正在尝试用C ++学习模板,我尝试的其中一件事就是编写一个地图函数,就像你通常在函数式语言中找到的那样。 The idea was something like this: 这个想法是这样的:

template <class X> X * myMap(X * func(X), X * array, int size)
    {
      X * temp;
      for(int i = 0, i < size, i++) {temp[i] = (*func)(array[i]);}
      return temp;
    }

But when I try to use this in: 但是当我尝试使用它时:

int test(int k) { return 2 * k;}
int main(void)
{
   int k[5] = {1,2,3,4,5};
   int *q = new int[5];
   q = myMap(&test, k, 5);
   for(int i=0; i<5; i++) {cout << q[i];}
   delete [] q;
   return 0;
}

I got a type mismatch error when compiling: 编译时出现类型不匹配错误:

 main.cpp:25: error: no matching function for call to ‘myMap(int (*)(int), int [5], int)’

I tried to change it to: 我试着把它改成:

int main(void)
{
   int *k = new int[5];
   int *q = new int[5];
   for(int i=0; i<5;i++) {k[i] = i;}
   q = myMap(&test, k, 5);
   for(int i=0; i<5; i++) {cout << q[i];}
   delete [] q;
   return 0;
}

The error message changes to: 错误消息更改为:

 main.cpp:26: error: no matching function for call to ‘myMap(int (*)(int), int*&, int)’

This is probably something very wrong, but I can't find where. 这可能是非常错误的,但我无法找到。

EDIT: The errors where: 1) I mistyped the pointer to function. 编辑:错误:1)我错误输入指向函数的指针。 It's X (*func)(X) instead of X * func(X) . 它是X(* func)(X)而不是X * func(X)。 2) forgot to allocate temp. 2)忘了分配temp。 Must do X * temp = new X[size] . 必须做X * temp = new X[size] 3) are there any more errors? 3)还有错误吗?

X * func(X) does not say what you think it says. X * func(X)没有说出你的想法。 You want X (*func)(X) . 你想要X (*func)(X)

You were very close. 你非常接近。 Just missing parens around X(*func)(X) . 只是在X(*func)(X)周围丢失了parens。 Couple other syntax errors, fixed here: 结合其他语法错误,修复此处:

#include <iostream>
using namespace std;


template <class X> X * myMap(X(*func)(X), X * array, int size)
    {
      X * temp;
      for(int i = 0; i < size; i++) {temp[i] = (*func)(array[i]);}
      return temp;
    }

int test(int k) { return 2 * k;}
int main(void)
{
   int k[5] = {1,2,3,4,5};
   int *q = new int[5];
   q = myMap(&test, k, 5);
   for(int i=0; i<5; i++) {cout << q[i];}
   delete [] q;
   return 0;
}

You're just getting the syntax for a function pointer wrong here. 你只是在这里得到函数指针的语法错误。 You want to say: 你想说:

template <class X> 
X* myMap(X (* func)(X), X * array, int size)
{
    ...
}

To make this function more generic, use a template parameter instead of a function pointer so you can use both plain function pointers and C++ function objects (functors). 要使此函数更通用,请使用模板参数而不是函数指针,以便可以使用普通函数指针 C ++函数对象(仿函数)。

template <class X, class F> 
X* myMap(F func, X * array, int size)
{
   ...
}

You're not calling your templated MyMap function, you'reattempting to call a non-templated MyMap function. 您没有调用模板化的MyMap函数,而是试图调用非模板化的MyMap函数。 Try q= MyMap(.....) 试试q = MyMap(.....)

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

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