array详解

array和vector大致是相同的,区别在于array的大小是固定的。不能增加和缩小。另外array的swap()函数和vector的swap()函数在算法复杂度上是有区别的,array.swap()函数是线性时间复杂度,vector,swap()是常量时间复杂度。在定义array的时候需要两个参数。

#include <iostream>
#include <array>
int main()
{
    std::array<int,3> arrayInt= {1,2,3};
    std::cout << "size : " << arrayInt.size() << std::endl;
    for(auto i : arrayInt)
    {
        std::cout << i << " ";
    }
    std::cout << std::endl;

    arrayInt.fill(3);
    std::cout << "fill(3) :  "  << std::endl;
    for(auto i : arrayInt)
    {
        std::cout << i << " ";
    }


    return 0;
}

结果是:

size : 3
1 2 3
fill(3) :
3 3 3

原文地址:https://www.cnblogs.com/boost/p/10397111.html