c++ - 在 C++ 中传递 std::array 作为参数

标签 c++ c++11

我想传递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)
    {
        // ...
    }
};

对了,最好传myarrconst & .

关于c++ - 在 C++ 中传递 std::array 作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60473113/

相关文章:

c++ - 时间类问题

c++ - 使用模板访问成员变量

c++ - 没有 unique_ptr 的 Poco::Net::HTTPClientSession 抛出异常

c++ - std::shared_ptr _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) 改变指针时

c++ - 将 mpl::fold 与占位符和我自己的结构事故一起使用

c++编译时字符串连接使用boost-mpl

c++ - alignas 说明符 vs __attribute__(aligned),c++11

c++ - 给定从键到索引的映射,将值 (mapped_type) 从映射复制到 vector

C++ 将模板类型作为参数传递给错误

c++ - C++中的rand()和srand()函数