设为首页 加入收藏

TOP

C++ - "array<>"数组容器 详解
2015-11-21 01:55:54 来源: 作者: 【 】 浏览:3
Tags:" array< > 容器 详解
?
数组容器, 是存储数组的容器, 是C类型数组的扩充, 可以使用迭代器进行操作;
例如"std::array", 需要注意的是, 如果直接进行赋值, "std::array ia = {1, 2, 3, 4, 5}; "
在GCC下会有警告: "missing braces around initializer for 'std::array::value_type [5] {aka int [5]}' [-Wmissing-braces]"
原因是与初始化数组的方式不符, 再加一组"{}"即可, 如: "std::array ia ={{1, 2, 3, 4, 5}};", 使参数满足int[5], 再进行赋值;
数组一般在初始化过程中赋值, 如果想替换已有的值, 一种方法是遍历所有的值, 较复杂;
另一种方法是通过复制去重新赋值, 实现快速赋值;
代码:
?
/* 
 * test.cpp 
 * 
 *  Created on: 2013.11.12 
 *      Author: Caroline 
 */  
  
/*eclipse cdt; gcc 4.7.1*/  
  
#include   
#include   
  
int main (void) {  
  
    std::array ia = {{1, 2, 3, 4, 5}};  
    for(const auto i : ia)  
        std::cout << i << " ";  
    std::cout << std::endl;  
  
    std::array ia2; // 空数组  
    //ia2 = {1, 2, 3, 4, 5}; //错误  
    ia2 = ia;  
    for(const auto i : ia2)  
        std::cout << i << " ";  
    std::cout << std::endl;  
  
    return 0;  
}  

?


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Don't Get Rooked UVA 639 下一篇关于“error C2871: 'std'..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: