美文网首页
6.15 An introduction to std::arr

6.15 An introduction to std::arr

作者: Closears | 来源:发表于2017-08-28 20:29 被阅读9次

    原完整教程链接: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;
    }
    

    相关文章

      网友评论

          本文标题:6.15 An introduction to std::arr

          本文链接:https://www.haomeiwen.com/subject/lqngdxtx.html