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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、Python介紹 Python的創(chuàng)始人為吉多·范羅蘇姆(Guido van Rossum)。1989年的圣誕節(jié)...
    EC君_王磊閱讀 65,550評(píng)論 3 33
  • 現(xiàn)在,一提起北京想必大家都知道是如今的這個(gè)被稱為帝都坐落在長城腳下的城市, 一說起南京大家也都知道是指那個(gè)位于在長...
    云卿先生閱讀 4,444評(píng)論 2 25
  • 細(xì)雨紛紛綠百花, 南風(fēng)處處熟枇杷。 興致勃勃去采摘, 神采亦亦提回家。
    噴泉閱讀 221評(píng)論 3 3
  • 清晨的陽光,映在簾布上。 不用拉開窗簾, 也能感覺到耀眼的光。 思量, 和這燦爛相稱的面容, 還...
    草木縈心閱讀 296評(píng)論 0 5

友情鏈接更多精彩內(nèi)容