6.15 An introduction to std::array

原完整教程链接:6.15 An introduction to std::array

  1. In previous lessons, we’ve talked at length about fixed and dynamic arrays. Although both are built right into the C++ language, they both have downsides: Fixed arrays decay into pointers, losing the array length information when they do, and dynamic arrays have messy deallocation issues and are challenging to resize without error.
  2. To address these issues, the C++ standard library includes functionality that makes array management easier, std::array and std::vector. We’ll examine std::array in this lesson, and std::vector in the next.

Introduced in C++11, std::array provides fixed array functionality that won’t decay when passed into a function. std::array is defined in the array header, inside the std namespace.

#include <iostream>
#include <array>
 
void printLength(const std::array<double, 5> &myarray)
{
    std::cout << "length: " << myarray.size();
}
 
int main()
{
    std::array<double, 5> myarray { 9.0, 7.2, 5.4, 3.6, 1.8 };
 
    printLength(myarray);
 
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、Python介绍 Python的创始人为吉多·范罗苏姆(Guido van Rossum)。1989年的圣诞节...
    EC君_王磊阅读 65,501评论 3 33
  • 现在,一提起北京想必大家都知道是如今的这个被称为帝都坐落在长城脚下的城市, 一说起南京大家也都知道是指那个位于在长...
    云卿先生阅读 4,366评论 2 25
  • 细雨纷纷绿百花, 南风处处熟枇杷。 兴致勃勃去采摘, 神采亦亦提回家。
    喷泉阅读 205评论 3 3
  • 清晨的阳光,映在帘布上。 不用拉开窗帘, 也能感觉到耀眼的光。 思量, 和这灿烂相称的面容, 还...
    草木萦心阅读 252评论 0 5