c++ - 为什么可以用花括号列表赋值给array类型?
怪我咯
怪我咯 2017-04-17 12:08:10
0
1
503
怪我咯
怪我咯

走同样的路,发现不同的人生

모든 응답(1)
小葫芦

楼主用的是支持c++11的编译器吧,

C++11新增了模板类array
{}初始化的方法,仅被最新的C++11标准支持,有个专门的术语:initializer-list

在不支持c++11的编译器上会报错, 比如在我的机器上报错如下:

   #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
 

 
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!