數組容器, 是存儲數組的容器, 是C類型數組的擴充, 可以使用迭代器進行操作;
例如"std::array<int, 5>", 需要注意的是, 如果直接進行賦值, "std::array<int, 5> ia = {1, 2, 3, 4, 5}; "
在GCC下會有警告: "missing braces around initializer for 'std::array<int, 5u>::value_type [5] {aka int [5]}' [-Wmissing-braces]"
原因是與初始化數組的方式不符, 再加一組"{}"即可, 如: "std::array<int, 5> ia ={{1, 2, 3, 4, 5}};", 使參數滿足int[5], 再進行賦值;
數組一般在初始化過程中賦值, 如果想替換已有的值, 一種方法是遍歷所有的值, 較復雜;
另一種方法是通過復制去重新賦值, 實現快速賦值;
代碼:
/*
* test.cpp
*
* Created on: 2013.11.12
* Author: Caroline
*/
/*eclipse cdt; gcc 4.7.1*/
#include <iostream>
#include <array>
int main (void) {
std::array<int, 5> ia = {{1, 2, 3, 4, 5}};
for(const auto i : ia)
std::cout << i << " ";
std::cout << std::endl;
std::array<int, 5> ia2; // 空數組
//ia2 = {1, 2, 3, 4, 5}; //錯誤
ia2 = ia;
for(const auto i : ia2)
std::cout << i << " ";
std::cout << std::endl;
return 0;
}
作者:csdn博客 Spike_King