• c++ array


    定义

    c++11添加了数组 array, 使用它需要包含 <array> 头文件,定义为:

    template<typename _Tp, std::size_t _Nm>
    struct array

    优点

    array不会像c语言中的数组一样会退化成T*。

    #include <iostream>
    #include <array>
    
    using namespace std;
    
    
    void func(array<int, 8> & a)
    {
        cout << sizeof(a) << endl;
    }
    
    int main()
    {
        array<int, 8> a;
    
        cout << sizeof(a) << endl;
    
        func(a);
    
        return 0;
    }

    输出:

    32
    32

    array 具有C风格的数组的优点,比如能够随机访问、知道大小、赋值,同时,支持C++容器的部分功能

    缺点

    默认情况下,array中的元素值是随机的

    不像vector容器,array的swap操作时间复杂度是 O(N)

    #include <iostream>
    #include <array>
    #include <algorithm>
    
    using namespace std;
    
    
    int main()
    {
        array<int, 8> a;
    
        for (int & e : a)
        {
            cout << e << endl; // 内部是随机值
        }
    
        a.fill(6);  // 全部赋值为6
    
        cout << a.size() << ' ' << a.max_size() << endl; // 8 8
        for_each(a.begin(), a.end(), [](int & i) {    // 遍历
            cout << i << ' ';
        });
    
        a.begin();
        a.end();
        a.cbegin();
        a.cend();
        a.rbegin();
        a.rend();
        a.crbegin();
        a.crend();
    
        cout << *a.data() << endl;
        cout << a.front() << ' ' << a.back() << endl;
        int *p = a.data();
    
        array<int, 8> b;
    
        a == b;
        a != b;
        a < b;
    
        cout << get<7>(a) << endl;
    
        return 0;
    }
  • 相关阅读:
    .NET-记一次架构优化实战与方案-梳理篇
    .net core实践系列之SSO-跨域实现
    Vue
    C# WPF
    开源框架
    开源框架
    开源框架
    开源框架
    WCF
    WCF
  • 原文地址:https://www.cnblogs.com/zuofaqi/p/10210480.html
Copyright © 2020-2023  润新知