简体   繁体   English

C ++:函数模板

[英]C++: Function templates

I have 2 2D arrays that represent a maze 我有2个代表迷宫的2D阵列

const char maze1[10][11] and const char maze2[20][21] const char maze1[10][11]const char maze2[20][21]

I'm trying to create 1 function to handle both mazes like so: 我正在尝试创建1个函数来处理这两个迷宫:

void solveMaze(maze[][])
{
}

and just pass the maze like solveMaze(maze1); 并像solveMaze(maze1);一样传递迷宫solveMaze(maze1);

How would I do this with function templates? 我如何使用功能模板执行此操作?

I recently asked this question already but explicitly asked not to use function templates because I wasn't sure on how to use it, but I would like to see how it would work. 我最近已经问过这个问题,但明确要求不使用功能模板,因为我不确定如何使用它,但我想看看它是如何工作的。 (hope this isn't "abusing" the system) (希望这不是“滥用”系统)

You do not need and should not use templates to solve this problem. 您不需要也不应该使用模板来解决此问题。 All you are doing is solving mazes of different sizes. 你所做的只是解决不同大小的迷宫。

Templates are for the generation of a number of classes/functions that use various types. 模板用于生成使用各种类型的许多类/函数。

Instead construct a class to store a maze. 而是构建一个类来存储迷宫。 This class should store the dimentsions of the maze and give access to the components of that maze. 这个类应该存储迷宫的内容,并允许访问该迷宫的组件。

First of all, it would be much simpler if you were using better arrays. 首先,如果你使用更好的数组会更简单。 The issue with C-arrays is that they have a tendency to decay to pointers easily, and once they do, the size is lost (and that, my dear, is pretty stupid as far as I am concerned...) C阵列的问题在于它们容易衰减到指针,一旦它们发生,它的大小就会丢失(就亲爱的而言,就我而言,这是非常愚蠢的......)

The choice then depends on whether you have fixed-size arrays or want dynamically-sized arrays: 然后选择取决于您是否具有固定大小的数组或是否需要动态大小的数组:

  • for fixed-size: std::array (or if unavailable boost::array ) 对于fixed-size: std::array (或者如果不可用的boost::array
  • for dynamically-size: std::vector for dynamic-size: std::vector

Since a template will make more sense in the std::array case, I'll suppose that is what you elected. 由于模板在std::array情况下更有意义,我想这就是你选的。

char const maze1[10][11]

is equivalent to 相当于

std::array<std::array<char, 11>, 10> const maze1

It's slightly more verbose, but std::array proposes regular member methods like .size() , .begin() , .end() , etc... and it can be passed in functions easily. 它稍微冗长一点,但是std::array提出了常规的成员方法,如.size() .begin() .end()等......它可以很容易地传递给函数。

Now, on to your template functions. 现在,转到您的模板功能。 The signature will simply be: 签名只是:

template <size_t M, size_t N>
void solveMaze(std::array<std::array<char, N>, M> const& maze);

However, despite your question, you more likely want not to use templates here (they are of little benefits). 但是,尽管您提出了问题,但您更可能不想在此处使用模板(它们没有什么好处)。 So I would advise using vector and a regular functions: 所以我建议使用vector和常规函数:

void solveMaze(std::vector< std::vector<char> > const& maze);
template<int w, int h>
void solveMaze(const char (&maze)[w][h])
{
    //can use w,h now
} 

There is no real support for multidimensional Arrays. 对多维数组没有真正的支持。 You should consider using a class with proper support for the dimensions. 您应该考虑使用具有适当尺寸支持的类。 The following does the trick 以下是诀窍

template<int N, int M>
void solveMaze(const char (&maze)[N][M]) {
    size_t n = N;
    size_t m = M;
    int x = 0;
}

int main(int argc, char *argv[])
{
    const char maze[3][2] = { { 0, 1} , {2, 3}, {4, 5} };
    solveMaze(maze);
    return 0;
}

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

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