c++ - std::array c++11 初始值设定项语法错误

标签 c++ arrays c++11 std operator-keyword

std::数组 我得到了

no match for ‘operator=’ in ‘myarr = {1, 5, 2, 3, 4}’

编译这段代码时出错

#include <iostream>
#include <array>

using namespace std;

int main(int argc, char const *argv[])
{
    array<int, 5> myarr;
    myarr = {1,5,2,3,4};

    for(auto i : myarr)
    {
        cout << i << endl;
    }

    return 0;
}

但是当我在同一行时它会编译

array<int, 5> myarr = {1,5,2,3,4};

如何在单独的行上赋值

我需要在类构造函数中赋值,我该怎么做?

class myclass
{
  myclass()
  {
    myarr = {1,2,3,4,5}; /// how to assign it   // it gives errors
  }
};

最佳答案

您需要两个而不是一对牙套。

myarray = {{1,2,3,4,5}};

关于c++ - std::array c++11 初始值设定项语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9314727/

相关文章:

arrays - Matlab:创建一个 symfun 数组

c++ - final 用于 C++ 中的优化吗?

c++ - 找到数字与给定数字相乘的最小数字

c++ - 如何在枚举中指定一个数字

c++ - 查找从未排序的数组中删除的一个和 N 个元素

c# - GDI+ DrawImage 在 C++ (Win32) 中比在 C# (WinForms) 中慢得多

arrays - 如何索引函数中的输出

javascript - 动态添加项目到ng-repeat数组javascript

c++ - 快速的字符串搜索

c++ - 将效果应用于输出而不是位图?