`
阿尔萨斯
  • 浏览: 4200523 次
社区版块
存档分类
最新评论

STL array的介绍(1)

 
阅读更多
class template
<array>

std::array

template < class T, size_t N > class array;
Array class
Arrays are fixed-size sequence containers: they hold a specific number of elements ordered in a strict linear sequence.

array是固定大小的序列容器,array存储固定大小的元素在连续的精确的序列中。


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.

在内部,一个array除了它的元素之外不保存其他的任何数据(甚至是其大小,因为其大小是array的模板参数,在编译的时候就已经确定了)。这是一种和平常利用[]语法来声明的数组一样高效的容器,该类仅仅只是在普通数组上加了一些成员以及全局方法,使array可以被当成标准容器来使用。


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 (seevectorfor a similar container that can be expanded).

和其他标准容器不一样的是,array具有固定的大小以及当它的元素超过(限制的大小)时它不能通过内存分配器来继续重分配,它是一个固定大小数组元素的封装,因此,他们不能自动增长或者收缩(比如vector).


Zero-sized arrays are valid, but they should not be dereferenced (membersfront,back, anddata).

大小为0的array是有效的,但是不应该被解除引用。(front,back,data方法).

例子:

#include <iostream>
#include <array>
using namespace std;
int main()
{
    array<int,0> ai;
    cout<<ai.front()<<endl;
    cout<<ai.back()<<endl;
    cout<<ai.data()<<endl;
}
运行截图:


可以看到,对其front,back得不到正确的值。


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.

和其他容器在标准库中的行为不同,交换两个array容器将分别交换两者的元素,这是一种线性操作,通常被认为是一种低效的行为。另一方面,这可以使原来的迭代器保持有效。

Another unique feature of array containers is that they can be treated astupleobjects: The<array>header overloads the getfunction to access the elements of the array as if it was atuple, as well as specializedtuple_sizeandtuple_element types.

array容器另一个独有的特性是可以被当成一个元组对象看待,array头文件重载了get方法可以使其像元组一样访问其元素,同样被特化的还有tuple_size以及tuple_element


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.
容器使用内置的constructor以及deconstructor来请求分配空间,其大小在编译时是一个常量,内存以及时间都不会超出。

Template parameters

T
Type of the elements contained.
Aliased as member typearray::value_type.
N
Size of the array, in terms of number of elements.
In the reference for thearraymember functions, these same names are assumed for the template parameters.

Member types

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

member typedefinitionnotes
value_type The first template parameter (T)
reference value_type&
const_reference const value_type&
pointer value_type*
const_pointer const value_type*
iterator arandom access iteratortovalue_type convertible toconst_iterator
const_iterator arandom access iteratortoconst value_type
reverse_iterator reverse_iterator<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<array>
Tuple element type for array(class template specialization )
tuple_size<array>
Tuple size traits for array(class template specialization )


—————————————————————————————————————————————————————————————————————————————

//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双

2014-8-25

于GDUT


——————————————————————————————————————————————————————————————————————————-———





分享到:
评论

相关推荐

    c++ 自己动手实现STL容器之array

    c++ 自己动手实现STL容器之array,本资源参考侯捷STL源码剖析一书,实现了STL容器、迭代器和内存管理等功能

    array:遵循STL精神的C ++多维数组

    array:遵循STL精神的C ++多维数组

    Make STL of 3D array (Optimal for 3d Printing):make_STL_of_Array 将包含在 3D 逻辑阵列中的体素化对象转换为 STL 表面网格,它是 3D 打印的最佳选择。-matlab开发

    make_STL_of_Array 将包含在 3D 逻辑数组中的体素化对象转换为 STL 表面网格,它是 3D 打印的最佳选择,因为最终 STL 中的面数尽可能少。 作者:Amir-Hosein Safari 联系方式:amirsfr5353@gmail.com 机构:马克斯...

    标准库STL_第1节_顺序容器

    详细介绍标准库STL中的容器:vector、list、forward_list、deque、string、array,讲解常用函数,并举例说明常见的用法和原理。

    C++ stl 源代码下载

    C++ 的 stl 源代码下载,熟悉C++stl库的原理和机制, 了解array等数据结构的内在实现

    新版STL修订内容 STL的新增特性

    新版STL修订内容,STL的新增特性: Constant迭代器 array类 tuple类 中新增函数 随机生成器类() 对sets及无序sets容器的改进 对maps及无序maps的改进 正则表达式 功能的改进及实用的头文件 加强指针管理类

    STL容器之array和vector.zip

    这里包含了array定长数组和vector动态数组的实现和测试代码

    C++STL学习笔记.pdf

    关于学习C++《STL模板库》以及学习《STL源码剖析》的学习笔记。

    VS2015中STL源码解析1(霜之小刀)

    主要介绍了array中的assign函数和fill函数的介绍以及完整实现。

    用C++在VS 2017 实现 DynamicArray-class 相关功能

    导师布置的基础作业,包括DynamicArray-class 动态数组类的基本操作,template类和STL的简单运用。为了方便更好的理解我把作业要求也放在里面了。相似代码可能网上也有的吧,就当做个备份了_(:з」∠)_

    类似STL接口数组

    STL中没有数组容器(Array),据说下一个版本中会加上;本人平时开发经常涉及到内存块的动态管理,所以写了这个模板类用于实现内存的动态创建、缩放(resize)、赋值、释放、指针类型访问等操作。

    stl数据结构.docx

    常用的数据结构有array(数组)、vector(向量)、list(列表)、tree(树)、stack(栈)、queue(队列)、hash table(散列表)、set(集合)、map(映射表)等等。这些数据结构按结构可以分为序列式(sequence)...

    stl-reader:用于解析STL(立体光刻)文件的Javascript库

    JavaScript库,用于将STL(立体光刻)文件解析为交错的顶点法线Float32Array,可以将其直接传递到WebGL或与诸如类的库使用以进行渲染。 服务器端 安装 npm install stl-reader 用法 var fs = require ( 'fs' ) ; ...

    c++11&14-STL要点汇总

    在c++里面不得不提的一个标准库,就是STL,STL包含很多实用的数据结构,如vector,list,map,set等都是我们常用的,而c++11也对STL做了一些补充,使得STL的内容越来越丰富,可选择的也越来越多了。 1. std::array 先看...

    C++语言中std::array的用法小结(神器用法)

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

    刷leetcode不用stl-leetcode:leetcode

    array 26 就地删除重复元素 直接用STL sort() unique() erase()做的 122 买卖股票的最佳时机 利润最大化就是每次波谷买入波峰卖出 189 旋转数组 暴力法 一次挪一步 挪k次 超时 STL reverse() 217 判断是否存在重复...

    C++之Boost::array用法简介

    首先,固定大小的数组还是很常见的,虽然stl提供了vector,但是vector作为动态可增长的数组,比静态数组多了一点开销,这在一些人看来是无法忍受的。c++里也需要提供固定大小容量的数组容器,当然,性能可以和普通...

    stl_sort.rar

    Classification Algorithm Program This example is the library algorithm sort's direct use of the internal built-in array d . . is how the ordinary pointer value is used as an iterator

    刷leetcode不用stl-CP-tricks:刷题心得

    刷leetcode不用stl CP-tricks 刷题心得 刷题技巧总结 string c++ STL: find_last_not_of, find_last_of可以用来找string中的单词 ...使用XOR来实现O(1)空间 : 使用二分。正确性比较难证明。 单调栈 注意在向前的

    Stl的list容器迭代器的用法1

    和array、vector、deque 容器的迭代器相比,list 容器迭代器最大的不同在于,其配备的迭代器类型为双向迭代器,而不再是随机访问迭代器。值得一提的

Global site tag (gtag.js) - Google Analytics