将不同大小的std :: array传递给函数

时间:2018-11-16 20:54:53

标签: c++

我在C语言中有此代码段:

int foo[5];
int bar[10];

Func(foo, _countof(foo));  // (_countof(x) is the size of the array x)
Func(foo, _countof(bar));

void Func(int p[], int count)
{
  for (int i = 0; i < count; i++)
  {
    // do something with p[i]
  }
}

我需要将其翻译成C ++

我的第一个想法是这样的:

std::array<int, 5> foo;
std::array<int, 10> bar;

Func(foo, foo.size());
// or even better:
Func(foo); 

到目前为止,还不错,但是现在我仍然使用Func函数,我需要传递std::array和大小,但是std::array<int, 5>与{ {1}}。

std::array<int, 10>

是否有一种优雅的方法可以使用void Func(std::array<int, ???> a, size_t count) { } 解决此问题?

我需要std::array成为功能模板吗?

我知道如何使用Func而不是std::vector来做到这一点。

0 个答案:

没有答案
相关问题