当前位置: 首页 > news >正文

重庆招标建设信息网站北京最新疫情

重庆招标建设信息网站,北京最新疫情,西安营销型网站制作,文明网站建设情况报告定义于头文件 <array> template< class T, std::size_t N > struct array;(C11 起) std::array 是封装固定大小数组的容器。 此容器是一个聚合类型&#xff0c;其语义等同于保有一个 C 风格数组 T[N] 作为其唯一非静态数据成员的结构体。不同于 C 风格数…
定义于头文件 <array>
template<

    class T,
    std::size_t N

> struct array;
(C++11 起)

 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>::empty

constexpr bool empty() const noexcept;

(C++11 起)
(C++20 前)

[[nodiscard]] constexpr bool empty() const noexcept;

(C++20 起)

 检查容器是否无元素,即是否 begin() == end() 。

参数

(无)

返回值

若容器为空则为 true ,否则为 false

复杂度

常数。

返回容纳的元素数

std::array<T,N>::size

size_type size() const noexcept;

(C++11 起)

返回容器中的元素数,即 std::distance(begin(), end()) 。

参数

(无)

返回值

容器中的元素数量。

复杂度

常数。

 

返回可容纳的最大元素数

std::array<T,N>::max_size

constexpr size_type max_size()  noexcept;

(C++11 起)
(C++14 前)

constexpr size_type max_size() const noexcept;

(C++14 起)

 返回根据系统或库实现限制的容器可保有的元素最大数量,即对于最大容器的 std::distance(begin(), end()) 。

参数

(无)

返回值

元素数量的最大值。

复杂度

常数。

注意

因为每个 std::array<T, N> 都是固定大小容器,故 max_size 返回的值等于 N (亦为 size 所返回的值)

操作

以指定值填充容器

std::array<T,N>::fill

void fill( const T& value );

(C++11 起)
(C++20 前)

constexpr void fill( const T& value );

(C++20 起)

 赋给定值 value 给容器中的所有元素。

参数

value-要赋给元素的值

返回值

(无)

复杂度

与容器大小成线性。

交换内容

std::array<T,N>::swap

void swap( array& other ) noexcept(/* see below */);

(C++11 起)
(C++20 前)

constexpr void swap( array& other ) noexcept(/* see below */);

(C++20 起)

将容器内容与 other 的内容交换。不导致迭代器和引用关联到别的容器。

参数

other-要与之交换内容的 array

返回值

(无)

异常

noexcept 规定:  

noexcept(noexcept(swap(std::declval<T&>(), std::declval<T&>())))

在以上表达式中,按照同 C++17 std::is_nothrow_swappable 特性所用的行为查找标识符 swap

(C++17 前)
noexcept 规定:  

noexcept(std::is_nothrow_swappable_v<T>)

(C++17 起)

对于零长 array ,noexcept 规定:  noexcept

复杂度

与容器大小成线性。

 

调用示例

#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;//检查容器是否无元素,即是否 begin() == end() 。std::cout << "array1 empty: " << array1.empty() << std::endl;std::array<Cell, 0> array2;std::cout << "array2 empty: " << array2.empty() << std::endl;std::cout << std::endl;//返回容器中的元素数,即 std::distance(begin(), end()) 。std::cout << "array1 contains " << array1.size() << " elements." << std::endl;std::cout << "array2 contains " << array2.size() << " elements." << std::endl;std::cout << std::endl;//返回根据系统或库实现限制的容器可保有的元素最大数量,即对于最大容器的 std::distance(begin(), end()) 。std::cout << std::endl;std::cout << "array1 max_size " << array1.max_size() << std::endl;std::cout << "array2 max_size " << array2.max_size() << std::endl;std::cout << std::endl;//赋给定值 value 给容器中的所有元素。Cell cell = generate();array1.fill(cell);std::cout << "array1.fill( " << cell << " )" << std::endl;std::cout << "array1:   ";std::copy(array1.begin(), array1.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;std::cout << std::endl;std::array<Cell, 6> array3;std::generate(array3.begin(), array3.end(), generate);std::cout << "swap before:" << std::endl;std::cout << "array1:   ";std::copy(array1.begin(), array1.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;std::cout << "array3:   ";std::copy(array3.begin(), array3.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;//将容器内容与 other 的内容交换。不导致迭代器和引用关联到别的容器。array1.swap(array3);std::cout << "swap after:" << std::endl;std::cout << "array1:   ";std::copy(array1.begin(), array1.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;std::cout << "array3:   ";std::copy(array3.begin(), array3.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;std::cout << std::endl;return 0;
}

输出

 

http://www.tj-hxxt.cn/news/91254.html

相关文章:

  • 长春做网站公司长春seo公司全国各城市疫情搜索高峰进度
  • 做设计在哪个网站上找高清图app推广接单平台
  • 建立内部网站需要多少钱百度联盟个人怎么接广告
  • 色系网站哪里有成品视频直播软件推荐哪个好用
  • php做学校网站免费央视网新闻
  • 那个网站都有做莱的图片网络营销优化培训
  • 手机小说网站源码长沙本地推广联系电话
  • 网站开发后端做那些郑州网站运营专业乐云seo
  • 瀑布流分享网站源代码下载新闻稿撰写
  • c 网站购物车怎么做河北网站seo
  • 学院网站建设工作会议电脑优化大师哪个好
  • 郑州做网站好seo咨询顾问
  • 360网站 备案seo营销怎么做
  • 如何做局域网网站湘潭网络推广
  • 哪些网站可以做易拉宝网站百度收录批量查询
  • 求个免费网站18款禁用看奶app入口
  • 做任务推广网站seo搜索优化排名
  • 云砺信息科技做网站谷歌seo排名技巧
  • 网站logo在哪里修改北京网站优化技术
  • 深圳市建设网络有限公司网站yahoo搜索引擎
  • 光明新区住房和建设局 官方网站营销推广的主要方式
  • 维护网站费用怎么做会计凭证谷歌推广新手教程
  • 建筑工程找活网站关键词挖掘工具爱网
  • 在常州 做兼职上什么网站在线一键生成网页
  • 现在做网站用什么工具知名网络营销推广
  • 深圳网站建设招聘百度seo排名原理
  • 企业网站报价表微信营销怎么做
  • 怎么做网页表白链接太原百度网站快速优化
  • 上海网络平台网站学推广网络营销去哪里
  • 跨境电商自建站是什么精准营销包括哪几个方面