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. word-wrap ,word-break 和white-space 的联系

    在工作中我遇到一个问题,其实功能也不复杂,就是上面有个textarea标签 ,里面输入内容,下面有个显示效果 ,有个条件就是 上面输入的什么格式(比如换行等等),下面显示的也是 什么格式.如下图: 这 ...

  2. C语言 · 字符转对比

    问题描述 给定两个仅由大写字母或小写字母组成的字符串(长度介于1到10之间),它们之间的关系是以下4中情况之一: 1:两个字符串长度不等.比如 Beijing 和 Hebei 2:两个字符串不仅长度相 ...

  3. Dapper扩展之~~~Dapper.Contrib

    平台之大势何人能挡? 带着你的Net飞奔吧!http://www.cnblogs.com/dunitian/p/4822808.html#skill 上一篇文章:Dapper逆天入门~强类型,动态类型 ...

  4. UE4新手引导入门教程

    请大家去这个地址下载:file:///D:/UE4%20Doc/虚幻4新手引导入门教程.pdf

  5. 记录一则Linux SSH的互信配置过程

    需求:四台Linux主机,IP地址为192.168.10.10/11/12/13,配置登录用户的互信 1.各节点ssh-keygen生成RSA密钥和公钥 ssh-keygen -q -t rsa -N ...

  6. Entity Framework 手动使用migration里面的up 和down方法。

    add-migration -IgnoreChanges 201606100717405_201606100645298_InitialCreate 执行这一句后 ,清空使用map生成的代码,个人不太 ...

  7. 【iOS】Xcode8+Swift3 纯代码模式实现 UICollectionView

    开发环境 macOS Sierra 10.12.Xcode 8.0,如下图所示: 总体思路 1.建立空白的storyboard用于呈现列表 2.实现自定义单个单元格(继承自:UICollectionV ...

  8. 在DevExpress程序中使用GridView直接录入数据的时候,增加列表选择的功能

    在我上篇随笔<在DevExpress程序中使用Winform分页控件直接录入数据并保存>中介绍了在GridView以及在其封装的分页控件上做数据的直接录入的处理,介绍情况下数据的保存和校验 ...

  9. ABP领域层

    1.实体Entites 1.1 概念 实体是DDD(领域驱动设计)的核心概念之一. 实体是具有唯一标识的ID且存储在数据库总.实体通常被映射成数据库中的一个表. 在ABP中,实体继承自Entity类. ...

  10. React Native 之 Text的使用

    前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...