美文网首页C++ STL
C++ STL 之 array(二)

C++ STL 之 array(二)

作者: 思想永不平凡 | 来源:发表于2020-01-09 15:27 被阅读0次

今天我们继续更新 C++ STL 中 array 容器的使用



array迭代器

array 容器定义了成员函数 begin() 和 end(),分别返回指向第一个元素和最后一个元素的下一个位置的随机访问迭代器。不只是 array 容器有 begin() 和 end(),其他容器也有。
迭代器有很多的功能,你可以使用 auto 来定义,也可以显示地定义。
示例如下:

#include <bits/stdc++.h>
using namespace std;

const int SIZE=10;
int main(){
    array<int,SIZE> data {1,1};
    for(int i=2;i<data.size();i++){
        data.at(i)=data.at(i-1)+data.at(i-2);
    }
    //使用auto
    for(auto&& d : data){
        cout<<d<<" ";
    }
    cout<<endl;
    //使用迭代器
    array<int,SIZE>::iterator start=data.begin();
    auto end=data.end();
    while(start!=end){
        cout<<*start<<" ";
        start++;
    }
    cout<<endl;
    return 0;
}

结果如下:


image.png

迭代器对象是由 array 对象的成员函数 begin() 和 end() 返回的。
此处,begin() 我们是使用 array<int,SIZE>::iterator 显示定义的,而 end() 则是使用 auto 定义,使用 auto 时需要注意迭代的实际类型。

同时我们也可以用全局的 begin() 和 end() 函数来从容器中获取迭代器。那么 start 和 end 可以如下定义:
示例如下:

#include <bits/stdc++.h>
using namespace std;

const int SIZE=10;
int main(){
    array<int,SIZE> data {1,1};
    for(int i=2;i<data.size();i++){
        data.at(i)=data.at(i-1)+data.at(i-2);
    }
    auto first = begin(data);
    auto last=end(data);
    while(first!=last){
        cout<<*first<<" ";
        first++;
    }
    cout<<endl;
    return 0;
}

结果如下:


image.png

成员函数 rbegin() 和 rend() 可以分别得到指向最一个元素和第一个元素前一个位置的反向迭代器。函数 crbegin() 和 crend() 可以返回 const 反向迭代器。我们可以用反向迭代器以逆序方式处理元素。

容器的部分迭代器如下:

迭代器 作用
begin() 返回指向容器中第一个元素的迭代器
end() 返回指向容器最后一个元素所在位置后一个位置的迭代器
rbegin() 返回指向最后一个元素的迭代器
rend() 返回指向第一个元素所在位置前一个位置的迭代器
cbegin() 和 begin() 功能相同,只不过在其基础上,增加了 const 属性,不能用于修改元素
cend() 和 end() 功能相同,只不过在其基础上,增加了 const 属性,不能用于修改元素
crbegin() 和 rbegin() 功能相同,只不过在其基础上,增加了 const 属性,不能用于修改元素
crend() 和 rend() 功能相同,只不过在其基础上,增加了 const 属性,不能用于修改元素

我们以 begin() 和 end(),rbegin() 和 rend() 迭代器为例。
示例如下:

#include <bits/stdc++.h>
using namespace std;

const int SIZE=10;
int main(){
    array<int,SIZE> data {1,1};
    for(int i=2;i<data.size();i++){
        data.at(i)=data.at(i-1)+data.at(i-2);
    }
    auto first = begin(data);
    auto last=end(data);
    while(first!=last){
        cout<<*first<<" ";
        first++;
    }
    cout<<endl;
    auto start = rbegin(data);
    auto finish = rend(data);
    while(start!=finish){
        cout<<*start<<" ";
        start++;
    }
    cout<<endl;
    return 0;
}

结果如下:


image.png

array 的其他功能

可以用比较运算符比较两个 array 容器,只要它们大小相同,保存的是相同类型的元素,而且这种类型的元素还要支持比较运算符。
示例如下:

#include <bits/stdc++.h>
using namespace std;

const int SIZE=5;
int main(){
    array<int,SIZE> data1 {1,2,3,4,5};
    array<int,SIZE> data2 {0};
    array<int,SIZE> data3 {-1,-2,-3,-4,-5};
    array<int,SIZE> data4 {};
    data4.fill(1);
    if(data1<data2){
        cout<<"data1 < data2."<<endl;
    }else{
        cout<<"data1 >= data2."<<endl;
    }
    if(data1==data3){
        cout<<"data1 and data3 are equal."<<endl;
    }else{
        cout<<"data1 and data3 are not equal."<<endl;
    }
    if(data1 >= data4){
        cout<<"data1 >= data4."<<endl;
    }else{
        cout<<"data1 < data4."<<endl;
    }
    return 0;
}

结果如下:


image.png

之前,我们介绍了 array 的访问,迭代器和比较,最后,我们介绍 array 容器的其他函数。

函数 作用
front() 返回第一个元素的引用
back() 返回最后一个元素的引用
swap() 交换两个容器的所有元素

示例如下:

#include <bits/stdc++.h>
using namespace std;

const int SIZE=10;
int main(){
    array<int,SIZE> my_data {1,1};
    for(int i=2;i<my_data.size();i++){
        my_data.at(i)=my_data.at(i-1)+my_data.at(i-2);
    }
    auto _front=my_data.front();
    int _back=my_data.back();
    cout<<"the first element is "<<_front<<endl;
    cout<<"the last element is "<<_back<<endl;
    cout<<endl;
    swap(_front,_back);
    cout<<"the _front is "<<_front<<endl;
    cout<<"the _back is "<<_back<<endl;
    return 0;
}

结果如下:


image.png

array 容器的使用就暂告一段落了,多多使用,自然就会熟练了。之后将继续更新 vector 容器的使用。

相关文章

  • C++ STL 之 array(二)

    今天我们继续更新 C++ STL 中 array 容器的使用 array迭代器 array 容器定义了成员函数 b...

  • C++ STL 之 array(一)

    最近刷题时一直用的是C++,而 stl(标准模板库) 是C++里面非常重要的程序库,为此我会持续更新stl内的相关...

  • [C++] STL 容器

    参考:[C++] STL 容器 (一) - 基本介紹[C++] STL 容器 (二) - Iterator 部分示例:

  • STL总结-容器

    C++标准库(STL)中的容器 1. 序列容器 1.1. array 1.2. vector 1.3 deque...

  • C++ STL 之 vectot(二)

    今天我们继续更新 C++ STL 中 vector 容器的使用 vector 迭代器使用 与 array 类似,v...

  • c++ STL

    c++ STL是开发中经常使用的。 常见的一些容器有 vector、array、deque、map、unorder...

  • C++ STL 之 vectot(一)

    今天我们将更新 C++ STL 中 vector 容器的使用,之前我们介绍了 array 容器的使用,其实容器之间...

  • 读书笔记17.06.03

    C++ STL:Listlist是C++标准模版库(STL,Standard Template Library)中...

  • nginx 动态数组

    ngx_array_t动态数组类似于C++语言STL库的vector容器,它用连续的内存存放着大小相同的元素(就像...

  • C++ STL 学习笔记

    C++ STL 学习笔记

网友评论

    本文标题:C++ STL 之 array(二)

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