C++ - "array<>"数组容器 详解

来源:互联网 发布:opencv python 掩膜 编辑:程序博客网 时间:2024/06/18 05:41

"array<>"数组容器 详解

 

本文地址: http://blog.csdn.net/caroline_wendy/article/details/15808673 

 

数组容器, 是存储数组的容器, 是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;}

原创粉丝点击