c++标准模板(STL)(std::array)(三)

这篇具有很好参考价值的文章主要介绍了c++标准模板(STL)(std::array)(三)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

定义于头文件 <array>
template<

    class T,
    std::size_t N

> struct array;
(C++11 起

 文章来源地址https://www.toymoban.com/news/detail-431817.html

std::array 是封装固定大小数组的容器。

此容器是一个聚合类型,其语义等同于保有一个 C 风格数组 T[N] 作为其唯一非静态数据成员的结构体。不同于 C 风格数组,它不会自动退化成 T* 。它能作为聚合类型聚合初始化,只要有至多 N 个能转换成 T 的初始化器: std::array<int, 3> a = {1,2,3}; 。

该结构体结合了 C 风格数组的性能、可访问性与容器的优点,比如可获取大小、支持赋值、随机访问迭代器等。

std::array 满足容器 (Container) 和可逆容器 (ReversibleContainer) 的要求,除了默认构造的 array 是非空的,以及进行交换的复杂度是线性,它满足连续容器 (ContiguousContainer) (C++17 起)的要求并部分满足序列容器 (SequenceContainer) 的要求。

当其长度为零时 arrayN == 0 )有特殊情况。此时, array.begin() == array.end() ,并拥有某个唯一值。在零长 array 上调用 front() 或 back() 是未定义的。

亦可将 array 当做拥有 N 个同类型元素的元组。

迭代器非法化

按照规则,指向 array 的迭代器在 array 的生存期间决不非法化。然而要注意,在 swap 时,迭代器将继续指向同一 array 的元素,并将改变元素的值。

迭代器

返回指向容器第一个元素的迭代器

std::array<T,N>::begin, std::array<T,N>::cbegin

iterator begin() noexcept;

(C++17 前)

constexpr iterator begin() noexcept;

(C++17 起)

const_iterator begin() const noexcept;

(C++17 前)

constexpr const_iterator begin() const noexcept;

(C++17 起)

const_iterator cbegin() const noexcept;

(C++17 前)

constexpr const_iterator cbegin() const noexcept;

(C++17 起)

 返回指向容器首元素的迭代器。

若容器为空,则返回的迭代器将等于 end() 。

c++标准模板(STL)(std::array)(三)

 

参数

(无)

返回值

指向首元素的迭代器。

复杂度

常数。

返回指向容器尾端的迭代器

std::array<T,N>::end, std::array<T,N>::cend

iterator end() noexcept;

(C++17 前)

constexpr iterator end() noexcept;

(C++17 起)

const_iterator end() const noexcept;

(C++17 前)

constexpr const_iterator end() const noexcept;

(C++17 起)

const_iterator cend() const noexcept;

(C++17 前)

constexpr const_iterator cend() const noexcept;

(C++17 起)

 返回指向容器末元素后一元素的迭代器。

此元素表现为占位符;试图访问它导致未定义行为。

c++标准模板(STL)(std::array)(三)

 

参数

(无)

返回值

指向后随最后元素的迭代器。

复杂度

常数。

 

返回指向容器最后元素的逆向迭代器

std::array<T,N>::rbegin, std::array<T,N>::crbegin

reverse_iterator rbegin() noexcept;

(C++17 前)

constexpr reverse_iterator rbegin() noexcept;

(C++17 起)

const_reverse_iterator rbegin() const noexcept;

(C++17 前)

constexpr const_reverse_iterator  rbegin() const noexcept;

(C++17 起)

const_reverse_iterator crbegin() const noexcept;

(C++17 前)

constexpr const_reverse_iterator crbegin() const noexcept;

(C++17 起)

 返回指向逆向容器首元素的逆向迭代器。它对应非逆向容器的末元素。

c++标准模板(STL)(std::array)(三)

参数

(无)

返回值

指向首元素的逆向迭代器。

复杂度

常数。

 

返回指向前端的逆向迭代器

std::array<T,N>::rend, std::array<T,N>::crend

reverse_iterator rend() noexcept;

(C++17 前)

constexpr reverse_iterator rend() noexcept;

(C++17 起)

const_reverse_iterator rend() const noexcept;

(C++17 前)

constexpr const_reverse_iterator rend() const noexcept;

(C++17 起)

const_reverse_iterator crend() const noexcept;

(C++17 前)

constexpr const_reverse_iterator crend() const noexcept;

(C++17 起)

返回指向逆向容器末元素后一元素的逆向迭代器。它对应非逆向容器首元素的前一元素。此元素表现为占位符,试图访问它导致未定义行为。

c++标准模板(STL)(std::array)(三) 

 参数

(无)

返回值

指向末元素后一元素的逆向迭代器。

复杂度

常数。

调用示例

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <time.h>
#include <array>

using namespace std;

struct Cell
{
    int x;
    int y;

    Cell() = default;
    Cell(int a, int b): x(a), y(b) {}

    Cell &operator +=(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator +(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator *(const Cell &cell)
    {
        x *= cell.x;
        y *= cell.y;
        return *this;
    }

    Cell &operator ++()
    {
        x += 1;
        y += 1;
        return *this;
    }


    bool operator <(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y < cell.y;
        }
        else
        {
            return x < cell.x;
        }
    }

    bool operator >(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y > cell.y;
        }
        else
        {
            return x > cell.x;
        }
    }

    bool operator ==(const Cell &cell) const
    {
        return x == cell.x && y == cell.y;
    }
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

using namespace std;

int main()
{
    std::cout << std::boolalpha;

    std::mt19937 g{std::random_device{}()};
    srand((unsigned)time(NULL));

    auto generate = []()
    {
        int n = std::rand() % 10 + 110;
        Cell cell{n, n};
        return cell;
    };

    //遵循聚合初始化的规则初始化 array (注意默认初始化可以导致非类的 T 的不确定值)
    std::array<Cell, 6> array1;
    std::cout << "array1:   ";
    std::copy(array1.begin(), array1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::generate(array1.begin(), array1.end(), generate);
    std::cout << "array1:   ";
    std::copy(array1.begin(), array1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    //返回指向容器首元素的迭代器。若容器为空,则返回的迭代器将等于 end() 。
    //返回指向容器末元素后一元素的迭代器。此元素表现为占位符;试图访问它导致未定义行为。
    for (std::array<Cell, 6>::iterator it = array1.begin(); it != array1.end(); it++)
    {
        *it = generate();
    }

    std::cout << "array1 const_iterator:   " << std::endl;
    for (std::array<Cell, 6>::const_iterator cit = array1.cbegin(); cit != array1.cend(); cit++)
    {
        std::cout << cit << " " << *cit << std::endl;
    }
    std::cout << std::endl;

    //返回指向逆向容器首元素的逆向迭代器。它对应非逆向容器的末元素。
    //返回指向逆向容器末元素后一元素的逆向迭代器。
    //它对应非逆向容器首元素的前一元素。此元素表现为占位符,试图访问它导致未定义行为。
    for (std::array<Cell, 6>::reverse_iterator  rit = array1.rbegin(); rit != array1.rend(); rit++)
    {
        *rit = generate();
    }

    std::cout << "array1 const_reverse_iterator:   " << std::endl;
    for (std::array<Cell, 6>::const_reverse_iterator crit = array1.crbegin(); crit != array1.crend(); crit++)
    {
        std::cout << "crit: " << " " << *crit << std::endl;
    }
    std::cout << std::endl;

    return 0;
}

输出

c++标准模板(STL)(std::array)(三)

 

到了这里,关于c++标准模板(STL)(std::array)(三)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • c++11 标准模板(STL)(std::bitset)(四)

    template std::size_t N class bitset;  类模板 bitset 表示一个 N 位的固定大小序列。可以用标准逻辑运算符操作位集,并将它与字符串和整数相互转换。 bitset 满足 可复制构造 (CopyConstructible) 及 可复制赋值 (CopyAssignable) 的要求。 模板形参 N - 要为 bitset 分配存储的位数 成员类型 refer

    2024年02月08日
    浏览(34)
  • c++11 标准模板(STL)(std::basic_stringstream)(四)

    template     class CharT,     class Traits = std::char_traitsCharT class basic_stringstream; (C++11 前) template     class CharT,     class Traits = std::char_traitsCharT,     class Allocator = std::allocatorCharT class basic_stringstream; (C++11 起) 类模板 std::basic_stringstream 实现基于字符串的流上的输入与输出操作。它等效

    2024年02月10日
    浏览(29)
  • c++11 标准模板(STL)(std::basic_ostream)(八)

    template     class CharT,     class Traits = std::char_traitsCharT class basic_ostream : virtual public std::basic_iosCharT, Traits 类模板 basic_ostream 提供字符流上的高层输出操作。受支持操作包含有格式输出(例如整数值)和无格式输出(例如生字符和字符数组)。此功能以 basic_streambuf 类所提供的接

    2024年02月12日
    浏览(35)
  • c++11 标准模板(STL)(std::basic_stringstream)(一)

    template     class CharT,     class Traits = std::char_traitsCharT class basic_stringstream; (C++11 前) template     class CharT,     class Traits = std::char_traitsCharT,     class Allocator = std::allocatorCharT class basic_stringstream; (C++11 起) 类模板 std::basic_stringstream 实现基于字符串的流上的输入与输出操作。它等效

    2024年02月10日
    浏览(29)
  • c++11 标准模板(STL)(std::priority_queue)(四)

    template     class T,     class Container = std::vectorT,     class Compare = std::lesstypename Container::value_type class priority_queue; priority_queue 是容器适配器,它提供常数时间的(默认)最大元素查找,对数代价的插入与释出。 可用用户提供的 Compare 更改顺序,例如,用 std::greaterT 将导致最小元

    2024年02月01日
    浏览(36)
  • c++11 标准模板(STL)(std::unordered_multimap)(九)

    template     class Key,     class T,     class Hash = std::hashKey,     class KeyEqual = std::equal_toKey,     class Allocator = std::allocator std::pairconst Key, T class unordered_multimap; (1) (C++11 起) namespace pmr {     template class Key, class T,               class Hash = std::hashKey,               class Pred = std::equa

    2023年04月09日
    浏览(31)
  • c++11 标准模板(STL)(std::basic_istream)(一)

    template     class CharT,     class Traits = std::char_traitsCharT class basic_istream : virtual public std::basic_iosCharT, Traits 类模板 basic_istream 提供字符流上的高层输入支持。受支持操作包含带格式的输入(例如整数值或空白符分隔的字符与字符串)和无格式输入(例如未处理字符和字符数组)。

    2024年02月13日
    浏览(25)
  • c++11 标准模板(STL)(std::basic_ifstream)(一)

    定义于头文件  fstream template     class CharT,     class Traits = std::char_traitsCharT class basic_ifstream : public std::basic_istreamCharT, Traits 类模板 basic_ifstream 实现文件流上的高层输入操作。它将 std::basic_istream 的高层接口赋予基于文件的流缓冲( std::basic_filebuf )。 std::basic_ifstream 的典型实

    2024年02月14日
    浏览(26)
  • c++11 标准模板(STL)(std::basic_istream)(三)

    template     class CharT,     class Traits = std::char_traitsCharT class basic_istream : virtual public std::basic_iosCharT, Traits  类模板 basic_istream 提供字符流上的高层输入支持。受支持操作包含带格式的输入(例如整数值或空白符分隔的字符与字符串)和无格式输入(例如未处理字符和字符数组)

    2024年02月13日
    浏览(31)
  • C++模板,STL(Standard Template Library)

    这篇文章的主要内容是C++中的 函数模板 、 类模板 、 STL的介绍 。 希望对C++爱好者有所帮助, 内容充实且干货 ,点赞+收藏防止找不到! 再次感谢每个读者和正在学习编程的朋友莅临! 更多优质内容请点击移驾: C++收录库:重生之C++启程(文章平均质量分93) 目录   1.模板

    2024年02月13日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包