std::array

template < class T, size_t N > class array;

Code Example

#include <iostream>
#include <array>
#include <cstring> using namespace std; int main(int argc, char **argv)
{
array<int, 5> intArr = {1,2,3,4,5};
for(auto it = intArr.begin(); it != intArr.end(); it++ )
{
cout << *it << "\t";
}
///< output: 1 2 3 4 5 cout << "\n";
///< r means reverse
for(auto rit = intArr.rbegin(); rit < intArr.rend(); rit++)
{
cout << *rit << "\t";
}
///< output: 5 4 3 2 1 cout << "\n";
///< c means const
for(auto cit = intArr.cbegin(); cit != intArr.cend(); cit++ )
{
cout << *cit << "\t";
}
///< output:1 2 3 4 5 cout << "\n";
for(auto crit = intArr.crbegin(); crit < intArr.crend(); crit++)
{
cout << *crit << "\t";
}
///< output:5 4 3 2 1 cout << "\n";
cout << "size of array:" << intArr.size() << "\n"; ///< output: 5
cout << "sizeof array:" << sizeof intArr << "\n"; ///< output: 5 cout << "\n";
array<char, 10> arrCh = {'a','b','c','d','e'};
cout << "size of array:" << arrCh.size() << "\n"; ///< output: 10
cout << "max_size of array" << arrCh.max_size() << "\n"; ///< output: 10 cout<< "\n";
array<int, 0> first;
array<int, 5> second;
cout << "first array:" << first.empty() << "\n";
///< output: 1, means true
cout << "second array:" << second.empty() << "\n";
///< output: 0, means false for(int i=0; i < second.size(); i++){
second[i]=i;
} for(auto it = second.begin(); it != second.end(); it++){
cout << *it << "\t";
}
///< output: 10 11 12 13 14 for(int i=0; i < second.size(); i++){
second.at(i) = i;
}
cout << "\n";
for(int i=0; i< second.size(); i++){
cout << second.at(i) << "\t";
}
///< output:1 2 3 4 5 array<int, 3> third = {1,2,3};
for(int &x:third){
cout << x << "\t";
}
///< output:1 2 3 cout << "\n";
cout << "array::front():" << third.front() << "\n"; ///< output: 1
cout << "array::back():" << third.back() << "\n"; ///< output: 3
cout <<"modify front and back\n";
third.front() == 11;
third.back() = 22;
cout << "array::front():" << third.front() << "\n"; ///< output: 11
cout << "array::back():" << third.back() << "\n"; ///< output: 22 const char * cstr = "Hello array";
array<char, 20> arrChar;
memcpy(arrChar.data(),cstr, strlen(cstr));
cout << arrChar.data() << "\n";
///< output: Hello array array<int,10> fourth;
fourth.fill(2);
for(int &x:fourth){
cout << x << "\t";
}
///< output: 2 2 2 2 2 2 2 2 2 2 array<int, 5> five = {1,2,3,4,5};
array<int, 5> six = {6,7,8,9,0};
five.swap(six);
cout << "\n";
for(int &x:five){
cout << x << "\t";
}
///< output: 6 7 8 9 0 cout << "\n";
for(int &x:six){
cout << x << "\t";
}
///< output:1 2 3 4 5 array<int,5> a = {1,2,3,4,5};
array<int,5> b = {1,2,3,4,5};
array<int,5> c = {5,4,3,2,1}; cout << "\n";
/** They are both true */
if( a == b ) cout << " a and b are equal!\n";
if( b!=c ) cout << "b and c are not equal!\n";
if( b < c ) cout << "b is less than c\n";
if( c > b ) cout << "c is greater than b\n";
if( a <= b ) cout << "a is less than or equal b\n";
if( a >= b ) cout << "ais greater than or equal b\n"; return 0;
}

Array class

Arrays are fixed-size sequence containers: they hold a specific number of elements ordered in a strict linear sequence.

Internally, an array does not keep any data other than the elements it contains (not even its size, which is a template parameter, fixed on compile time). It is as efficient in terms of storage size as an ordinary array declared with the language's bracket syntax ([]). This class merely adds a layer of member and global functions to it, so that arrays can be used as standard containers.

Unlike the other standard containers, arrays have a fixed size and do not manage the allocation of its elements through an allocator: they are an aggregate type encapsulating a fixed-size array of elements. Therefore,they cannot be expanded or contracted dynamically (see vector for a similar container that can be expanded).Zero-sized arrays are valid, but they should not be dereferenced (members front, back, and data).

Unlike with the other containers in the Standard Library, swapping two array containers is a linear operation that involves swapping all the elements in the ranges individually, which generally is a considerably less efficient

operation. On the other side, this allows the iterators to elements in both containers to keep their original container association.

Another unique feature of array containers is that they can be treated as tuple objects: The header overloads the get function to access the elements of the array as if it was a tuple, as well as specialized tuple_size

and tuple_element types.

Container properties

  • Sequence:Elements in sequence containers are ordered in a strict linear sequence.Individual elements are accessed by their position in this sequence.
  • Contiguous storage:The elements are stored in contiguous memory locations, allowing constant time random access to elements. Pointers to an element can be offset to access other elements.
  • Fixed-size aggregate:The container uses implicit constructors and destructors to allocate the required space statically. Its size is compile-time constant. No memory or time overhead.

Template parameters

  • T:Type of the elements contained. Aliased as member type array::value_type.
  • N:Size of the array, in terms of number of elements.

In the reference for the array member functions, these same names are assumed for the template parameters.

Member types

The following aliases are member types of array. They are widely used as parameter and return types by member functions:

member typedefinitio notes
value_type The first template parameter (T)
reference value_type&
const_reference const value_type&
pointer value_type*
const_pointer const value_type*
iterator a random access iterator to value_type convertible to const_iterator
const_iterator a random access iterator to const value_type
reverse_iterator reverse_iterator
const_reverse_iterator reverse_iterator<const_iterator>
size_type size_t unsigned integral type
difference_type ptrdiff_t

signed integral type

Member functions

Iterators
  • begin: Return iterator to beginning (public member function )
  • end: Return iterator to end (public member function )
  • rbegin: Return reverse iterator to reverse beginning (public member function )
  • rend: Return reverse iterator to reverse end (public member function )
  • cbegin: Return const_iterator to beginning (public member function )
  • cend: Return const_iterator to end (public member function )
  • crbegin: Return const_reverse_iterator to reverse beginning (public member function )
  • crend: Return const_reverse_iterator to reverse end (public member function )
Capacity
  • size: Return size (public member function )
  • max_size: Return maximum size (public member function )
  • empty: Test whether array is empty (public member function )
Element access
  • operator[]: Access element (public member function )
  • at: Access element (public member function )
  • front: Access first element (public member function )
  • back: Access last element (public member function )
  • data: Get pointer to data (public member function )
Modifiers
  • fill: Fill array with value (public member function )
  • swap: Swap content (public member function )
Non-member function overloads
  • get (array): Get element (tuple interface) (function template )
  • relational operators (array):Relational operators for array(function template )
Non-member class specializations
  • tuple_element: Tuple element type for array (class template specialization )
  • tuple_size: Tuple size traits for array (class template specialization )

参考文献

cplusplus


C++ std::array的更多相关文章

  1. c++编译错误C2971:"std::array":array_size:包含非静态存储不能用作废类型参数;参见“std::array”的声明

    在Qt5中这段代码编写有两种方式:一个编译成功,一个失败 成功版本: static constexpr size_t block_size = 0x2000;//8KB static constexp ...

  2. std::array中的std::get<n>()

    模板函数std::get<n>()是一个辅助函数,它能够获取到容器的第 n 个元素.模板参数的实参必须是一个在编译时可以确定的常量表达式,编译时会对它检查. get<n>()模 ...

  3. 将std::array转换成std::tuple

    template<typename Array, std::size_t... Index> decltype(auto) array2tuple_impl(const Array& ...

  4. std::array,std::vector,基于范围的for循环

    std::array除了有传统数组支持随机访问.效率高.存储大小固定等特点外,还支持迭代器访问.获取容量.获得原始指针等高级功能.而且它还不会退化成指针T *给开发人员造成困惑. for( 元素名变量 ...

  5. C++ Arrays, std::array, std::vector 总结

    原文来自: https://shendrick.net/Coding%20Tips/2015/03/15/cpparrayvsvector.html @Seth Hendrick Original a ...

  6. C++语言中std::array的神奇用法总结,你需要知道!

    摘要:在这篇文章里,将从各个角度介绍下std::array的用法,希望能带来一些启发. td::array是在C++11标准中增加的STL容器,它的设计目的是提供与原生数组类似的功能与性能.也正因此, ...

  7. C++ std::array 基本用法

    #include <iostream> #include <string> #include <array> using namespace std; // htt ...

  8. 源码阅读笔记 - 2 std::vector (2) 关于Allocator Aware Container特性

    所有的STL容器,都保存一个或默认,或由用户提供的allocator的实例,用来提供对象内存分配和构造的方法(除了std::array),这样的容器,被称作Allocator Aware Contai ...

  9. [翻译] C++ STL容器参考手册(第一章 <array>)

    返回总册 本章节原文:http://www.cplusplus.com/reference/array/array/ 1. std::array (C++11支持) template < cla ...

随机推荐

  1. SQL Server 大数据搬迁之文件组备份还原实战

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 解决方案(Solution) 搬迁步骤(Procedure) 搬迁脚本(SQL Codes) ...

  2. Linux 内核概述 - Linux Kernel

    Linux 内核学习笔记整理. Unix unix 已有40历史,但计算机科学家仍认为其是现存操作系统中最大和最优秀的系统,它已成为一种传奇的存在,历经时间的考验却依然声名不坠. 1973 年,在用 ...

  3. 百度推出新技术 MIP,网页加载更快,广告呢?

    我们在2016年年初推出了MIP,帮助移动页面加速(原理).内测数据表明,MIP页面在1s内加载完成.现在已经有十多家网站加入MIP项目,有更多的网站正在加入中.在我们收到的反馈中,大部分都提到了广告 ...

  4. Code Review 程序员的寄望与哀伤

    一个程序员,他写完了代码,在测试环境通过了测试,然后他把它发布到了线上生产环境,但很快就发现在生产环境上出了问题,有潜在的 bug. 事后分析,是生产环境的一些微妙差异,使得这种 bug 场景在线下测 ...

  5. Socket聊天程序——客户端

    写在前面: 上周末抽点时间把自己写的一个简单Socket聊天程序的初始设计和服务端细化设计记录了一下,周二终于等来毕业前考的软考证书,然后接下来就是在加班的日子度过了,今天正好周五,打算把客户端的详细 ...

  6. java EE设计模式简介

    1.何为设计模式 设计模式提供了对常见应用设计问题的解决方案.在面向对象的编程中,设计模式通常在解决与对象创建和交互相关的问题,而非整体软件架构所面对的大规模问题,它们以样板代码的形式提供了通用的解决 ...

  7. 解决cookie跨域访问

    一.前言 随着项目模块越来越多,很多模块现在都是独立部署.模块之间的交流有时可能会通过cookie来完成.比如说门户和应用,分别部署在不同的机器或者web容器中,假如用户登陆之后会在浏览器客户端写入c ...

  8. C#中如何在Excel工作表创建混合型图表

    在进行图表分析的时候,我们可能需要在一张图表呈现两个或多个样式的图表,以便更加清晰.直观地查看不同的数据大小和变化趋势.在这篇文章中,我将分享C#中如何在一张图表中创建不同的图表类型,其中包括如何在同 ...

  9. WebGIS中等值线前端生成绘制简析

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 等值线是GIS制图中常见的功能,一般有两种思路:一种是先进行插 ...

  10. winform异步加载数据到界面

    做一个学习记录. 有两个需求: 1.点击按钮,异步加载数据,不卡顿UI. 2.把获取的数据加载到gridview上面. 对于需求1,2,代码如下: public delegate void ShowD ...