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

"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;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ElminsterAumar

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值