序列型容器之array

https://en.cppreference.com/w/cpp/container

  • 头文件

<array>

  • 声明

template<
    class T,
    std::size_t N
> struct array;

  • 描述

std::array 是一个容器,它封装了固定大小的数组。

这个容器是与C风格的数组T[N]具有相同的语义结构的集合类型。与C风格数组不同的是,它不能直接自动变成T*。作为一个集合类型,它可以由集合进行初始化: std::array<int, 3> a = {1,2,3};.

这个结构结合了C风格的高性能和方便性和标准容器收益,例如,知道它的大小、它支持的赋值、随机访问iterators,等等。

一个特殊情况,数组大小为0.这种情况下,array.begin() == array.end(),且是某个唯一值。对0大小的数组进行front()或back()的调用结果未定义。

  • 迭代

值得说明的是,在“swap”期间,迭代器会继续指向相同的数组元素,并改变它的值。

  • 成员类型
类型定义
value_typeT
size_type    std::size_t
difference_type std::ptrdiff_t
reference value_type&
const_referenceconst value_type&
pointer value_type*
const_pointerconst value_type*
iterator

legacyRandomAccessIterator and ConstexprIterator

(since C++20)that is a LiteralType (since C++17)

const_iterator Constant LegacyRandomAccessIterator and ConstexprIterator (since C++20)that is a LiteralType (since C++17)
reverse_iteratorstd::reverse_iterator<iterator>
const_reverse_iteratorstd::reverse_iterator<const_iterator>
  • 成员操作

         元素访问

ataccess specified element with bounds checking
(public member function)
operator[]access specified element
(public member function)
frontaccess the first element
(public member function)
backaccess the last element
(public member function)
datadirect access to the underlying array
(public member function).获取数组的数据存放的内存地址

      迭代器

begin
cbegin
returns an iterator to the beginning
(public member function)
end
cend
returns an iterator to the end
(public member function)
rbegin
crbegin
returns a reverse iterator to the beginning
(public member function)
  

     指标相关操作

emptychecks whether the container is empty
(public member function)
sizereturns the number of elements
(public member function)
max_sizereturns the maximum possible number of elements
(public member function)

    数值操作

fillvoid fill( const T& value );fill the container with specified value
(public member function)
swapvoid swap( array& other ) noexcept(/* see below */);swaps the contents
(public member function)
  • 举例

#include <string>
#include <iterator>
#include <iostream>
#include <algorithm>
#include <array>
 
int main()
{
    // construction uses aggregate initialization
    std::array<int, 3> a1{ {1, 2, 3} }; // double-braces required in C++11 prior to the CWG 1270 revision
                                        // (not needed in C++11 after the revision and in C++14 and beyond)
    std::array<int, 3> a2 = {1, 2, 3};  // never required after =
    std::array<std::string, 2> a3 = { std::string("a"), "b" };
 
    // container operations are supported
    std::sort(a1.begin(), a1.end());
    std::reverse_copy(a2.begin(), a2.end(), 
                      std::ostream_iterator<int>(std::cout, " "));
 
    std::cout << '\n';
 
    // ranged for loop is supported
    for(const auto& s: a3)
        std::cout << s << ' ';
}
Output:

3 2 1 
a b

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值