C++ STL库复习(3) array

array<T,N> 模板定义了一种相当于标准数组的容器类型。它是一个有 N 个 T 类型元素的固定序列。除了需要指定元素的类型和个数之外,它和常规数组没有太大的差别,不能增加或删除元素。
和标准数组相比,array 容器的额外幵销很小,但提供了两个优点:如果使用 at(),当用一个非法的索引访问数组元素时,能够被检测到,因为容器知道它有多少个元素,这也就意味着数组容器可以作为参数传给函数,而不再需要单独去指定数组元素的个数。

array 数组容器的创建

在使用array容器时,需要在源文件中添加头文件 ,参考实例

std::array<double,100> data;                                               // 定义了一个具有100个double类型的数组,且未对数组初始化
std::array<double, 100> data {};                                          // 定义了一个具有100个double类型的数组,并将数组初始化为0或者默认元素类型等效的值
std::array<double, 10> values {0.5, 1.0, 1.5, 2.0};         // 定义了一个具有10个double类型的数组,将数组前四位初始化为指定值,后面的数组值初始化为0或者默认元素类型等效的值

在这里插入图片描述

array元素的获取

通过调用数组对象的成员函数 fill(),可以将所有数组array中的元素设成给定值。例如:

values.fill(3.1415926);

可以通过在方括号中使用索引表达式汸问和使用数组容器的元素,这和标准数组的访问方式相同,例如:

values[4] = values[3] + 2.0*values[1];

像这样使用索引时,因为没有做任何边界检査,所以,如果使用越界的索引值去访问或存储元素,就不会被检测到。为了能够检查越界索引值,应该使用成员函数 at():

values.at (4) = values.at(3) + 2.0*values.at(1);

这和前一条语句的功能相同。但是,这两种方式的不同是,使用 at() 时使用的索引是一个越界值时,会抛出一个 std::out_of_rang 异常。所以,能使用 at() 就使用 at() ,尽量不要使用方括号。

size() 函数可以返回 size_t 类型的元素个数值。
empty() 函数可以判断 array 数组容器是否为空,为空返回 true ,否则 false 。不过,实际情况是,如果一个array数组容器为空,很没必要,因为 array 一旦创建,元素个数就是固定的,除非说,创建array的时候,第二个参数赋值为0 ,那这样就没有创建的必要了。有 empty() 这个函数的主要原因,是为了和后面的可删除容器保持一致。
数组容器的成员函数 front() 和 back() 分别返回第一个元素和最后一个元素的引用。
get() 函数,能够获取到容器的第n个元素,get() 模板提供了一种不需要在运行时检查,但能用安全的索引值访问元素的方式。

std::array<std::string, 5> words {"one","two","three","four","five" };
std::cout << std::get<3>(words) << std::endl;                                                       // Output words[3]
std::cout << std::get<6>(words) << std::endl;                                                       // Compiler error message!

array 迭代器

数组模板定义了 begin() 和 end() 函数,反别返回指向第一个数组元素和最后一个元素位置 + 1 的随机访问迭代器。

另一种方式就是,使用全局 begin() 和 end() 函数来从容器中获取迭代器,因为这样比较通用

#include <iostream> // For standard streams
#include <array>    // For array<T,N>

int main()
{
    std::array<double,5> height{1,2,3,4,5};
    //auto first = height.begin();
    //auto end = height.end();
    auto first = std::begin(height);
    auto end = std::end(height);                         //  这两种方式都可以
    double sum = 0;

    while(first != end)
    {
        sum += *first;
        first++;
        std::cout << sum <<std::endl;    
    }
   
    return 0;
}

反向迭代器 rbegin() 和 rend() 就不赘述了,就是从尾部向头部开始,反过来用就是了。
此外,还有cbegin() 和 cend(),它们可以返回 const 迭代器。函数 crbegin() 和 crend() 可以返回 const 反向迭代器。

array元素的比较和赋值

只要array中元素的个数,类型相同,并且这种类型的元素支持比较运算符的话,就可以用比较运算符进行数组的比较运算。
另外,至于赋值操作,array不像标准数组,只要它们存放的是相同类型、相同个数的元素,就可以将一个数组容器赋给另一个。
参考代码

#include <iostream> // For standard streams
#include <array>    // For array<T,N>

int main()
{
    std::array<double,4> these {1.0, 2.0, 3.0, 4.0};
    std::array<double,4> those {1.0, 2.0, 3.0, 4.0};
    std::array<double,4> them {1.0, 3.0, 3.0, 2.0};

    if (these == those) 
        std::cout << "these and those are equal." << std::endl;
    if (those != them) 
        std::cout << "those and them are not equal."<< std::endl;
    if (those < them) 
        std::cout << "those are less than them."<< std::endl;
    if (them > those) 
        std::cout << "them are greater than those." << std::endl;   

    them = those;
    double tmp;
    auto first = them.begin(); 
    auto end = them.end();
    while(first != end) 
    {
        tmp = *first;
        std::cout << tmp <<std::endl;
        first++;
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值