我想传递std::array
作为函数的参数,我找不到正确的方法。
我不是在谈论普通的 C 数组(例如 int arr[2]={1,3};
)。
我说的是std::array
类,可从 C++ 11 开始使用。
示例代码:
#include <iostream>
#include <array>
using namespace std;
class test
{
void function(array<int> myarr)
{
// .......some code..........
}
};
如何通过 std::array
到一个函数,如 std::array
采用两个模板参数:std::array<class T, std::size_t N>
,但是在将其作为参数传递时我不想指定大小?
最佳答案
不知道 std::array<>
的第二个模板参数意味着你的test
类也应该模板化。
template <std::size_t N>
class test
{
void function(const std::array<int, N> & myarr)
{
// ...
}
};
对了,最好传myarr
如const &
.
关于c++ - 在 C++ 中传递 std::array 作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60473113/