search

Home  >  Q&A  >  body text

c++ - 为什么可以用花括号列表赋值给array类型?

怪我咯怪我咯2807 days ago572

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-04-17 12:10:10

    Is the poster using a compiler that supports c++11?

    C++11 adds a new method of initializing the template class array
    {}, which is only supported by the latest C++11 standard. There is a special term: initializer-list

    An error will be reported on a compiler that does not support c++11. For example, the error reported on my machine is as follows:

       #include <array>
       #include <iostream>
       using namespace std;
       
       int main()
       {
           array<int, 3> a;
           a = {0, 1, 2}; 
           cout<<a[1]<<endl;
           return 0;
       }

    In file included from /usr/include/c++/4.9/array:35:0,

    from huakuohao.cpp:1:
    /usr/include/c++/4.9/bits/c++0x_warning.h:32:2: error: #error This file requires     
    compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
     #error This file requires compiler and library support for the \
      ^
    huakuohao.cpp: In function ‘int main()’:
    huakuohao.cpp:7:5: error: ‘array’ was not declared in this scope
    huakuohao.cpp:8:17: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
     
    
     

    reply
    0
  • Cancelreply