STL::array资料

Array容器的相关知识,array是一个顺序容器,和其他标准容器相比它的特点是容器的大小固定,顺序存储。

1:array的构造函数

array();

array(const array & right);

2:array的成员变量

Type DefinitionDescription

array::const_iteratorThe type of a  constant iterator for the controlled sequence.

array::const_pointerThe type of a  constant pointer to an element.

array::const_referenceThe type of a  constant reference to an element.

array::const_reverse_iteratorThe type of a  constant reverse iterator for the controlled sequence.

array::difference_typeThe type of a  signed distance between two elements.

array::iteratorThe type of an  iterator for the controlled sequence.

array::pointerThe type of a  pointer to an element.

array::referenceThe type of a  reference to an element.

array::reverse_iteratorThe type of a  reverse iterator for the controlled sequence.

array::size_typeThe type of an  unsigned distance between two elements.

array::value_typeThe type of an  element.

3:array的关于迭代器的成员函数

Iterators

beginReturn iterator to beginning(public member function )

endReturn iterator to end(public member function )

rbeginReturn reverseiterator to reverse beginning(public member function )

rendReturnreverse iterator to reverse end(public member function )

cbeginReturnconst_iterator to beginning(public member function )

cendReturnconst_iterator to end(public member function )

crbeginReturnconst_reverse_iterator to reverse beginning(public member

function )

crendReturnconst_reverse_iterator to reverse end(public member

function )

这些东西和list中迭代器都类似,在list篇中已经做过大量介绍,这里就不再啰嗦了。

4:array中关于容量的函数

Capacity

sizeReturnsize(public member function )

max_sizeReturnmaximum size(public member function )

emptyTestwhether array is empty(public member function )

4.1 size()函数的用法,从结果中可以看出array容量的一些端倪来。

size_type size() const;

#include

#include

intmain ()

{

std::array myints;

std::cout <<"size of myints: "<< myints.size() << std::endl;

std::cout <<"sizeof(myints): "<

return0;

}

结果:

size of myints: 5

sizeof(myints): 20

4.2 max_size()函数的用法,说明这个函数和list中的max_size()的不同

size_type max_size() const;

#include

#include

intmain ()

{

std::array myints;

std::cout <<"size of myints: "<< myints.size() <<'\n';

std::cout <<"max_size of myints: "<< myints.max_size() <<'\n';

return0;

}

结果:

size of myints: 10

max_size of myints: 10

4.3 empty函数

bool empty() const;

5.array中关于元素操作的函数

Element access

operator[]Accesselement(public member function )

atAccesselement(public member function )

frontAccessfirst element(public member function )

backAccesslast element(public member function )

dataGetpointer to data(public member function )

5.1 operator[]操作符

reference operator[](size_type off);

const_reference operator[](size_type off) const;

示例:

#include

#include

intmain ()

{

std::array myarray;

unsignedinti;

for(i=0; i<10; i++) myarray[i]=i;// assign some values:

std::cout <<"myarray contains:";// print content

for(i=0; i<10; i++)

std::cout <<' '<< myarray[i];

std::cout <<'\n';

return0;

}

输出结果:

myarray contains: 0 1 2 3 4 5 6 7 8 9

5.2 at()函数的用法

reference at(size_type off);

const_reference at(size_type off) const;

用法:

#include

#include

intmain ()

{

std::array myarray;

for(inti=0; i<10; i++)

myarray.at(i) = i+1;// assign some values:

std::cout <<"myarray contains:"// print content:;

for(inti=0; i<10; i++)

std::cout <<' '<< myarray.at(i);

std::cout <<'\n';

return0;

}

输出结果:

myarray contains: 1 2 3 4 5 6 7 8 9 10

5.3 front函数的用法

reference front();

const_reference front() const;

示例:

#include

#include

intmain ()

{

std::array myarray = {2, 16, 77};

std::cout <<"front is: "<< myarray.front() << std::endl;// 2

std::cout <<"back is: "<< myarray.back() << std::endl;// 77

myarray.front() = 100;

std::cout <<"myarray now contains:";

for(int& x : myarray ) std::cout <<' '<< x;

std::cout <<'\n';

return0;

}

结果:

front is: 2

back is: 77

myarray now contains: 100 16 77

5.4 back函数的用法

reference back();

const_reference back() const;

关于例子,在5.3中的例子已经包含了

5.5 data函数的用法

Ty *data();

const Ty *data() const;

返回值指向第一个元素的指针,因为是顺序存储,所以知道了首元素的指针就可以知道所有元素了。

#include

#include

#include

intmain ()

{

constchar* cstr ="Test string";

std::array charray;

std::memcpy (charray.data(),cstr,12);

std::cout << charray.data() <<'\n';

return0;

}

结果:

Test string

6修改元素的函数

Modifiers

fillFill arraywith value(public member function )

swapSwap content(public member function )

6.1 fill函数的用法

void fill(const Type& _Val);

函数将array中所有的元素替换成_val

#include

#include

intmain () {

std::array myarray;

myarray.fill(5);

std::cout <<"myarray contains:";

for(int& x : myarray) { std::cout <<' '<< x; }

std::cout <<'\n';

return0;

}

结果:

myarray contains: 5 5 5 5 5 5

6.2 swap函数的用法

void swap(array& right);

交换两个具有相同长度的array,切记两个array的长度必须一致

例子:

#include

#include

intmain ()

{

std::array first = {10, 20, 30, 40, 50};

std::array second = {11, 22, 33, 44, 55};

first.swap (second);

std::cout <<"first:";

for(int& x : first) std::cout <<' '<< x;

std::cout <<'\n';

std::cout <<"second:";

for(int& x : second) std::cout <<' '<< x;

std::cout <<'\n';

return0;

}

结果:

first contains: 11 22 33 44 55

second contains: 10 20 30 40 50

附加头文件中其他跟array类无关的类以及函数

1:tuple_element类

template

class tuple_element > {

typedef Ty type;

};

这个类就是获取array中第几个元素的值,示例如下:

#include

#include

typedef std::array Myarray;

int main()

{

Myarray c0 = {0, 1, 2, 3};

// display contents " 0 1 2 3"

for (Myarray::const_iterator it = c0.begin();

it != c0.end(); ++it)

std::cout << " " << *it;

std::cout << std::endl;

// display first element " 0"

std::tuple_element<0, Myarray>::type val = c0.front();

std::cout << " " << val;

std::cout << std::endl;

return (0);

}

结果:

0 1 2 3

0

2:tuple_size类

template

class tuple_size > {

static const unsigned value = N;

};

这个类就是获取array数组的大小,示例

#include

#include

typedef std::array Myarray;

int main()

{

Myarray c0 = {0, 1, 2, 3};

// display contents " 0 1 2 3"

for (Myarray::const_iterator it = c0.begin();

it != c0.end(); ++it)

std::cout << " " << *it;

std::cout << std::endl;

// display size " 4"

std::cout << " " << std::tuple_size::value;

std::cout << std::endl;

return (0);

}

结果:

0 1 2 3

4

3:get函数的用法

template

Ty& get(array& arr);

template

const Ty& get(const array& arr);

这个函数就是返回array[id]的索引,示例

#include

#include

typedef std::array Myarray;

int main()

{

Myarray c0 = {0, 1, 2, 3};

// display contents " 0 1 2 3"

for (Myarray::const_iterator it = c0.begin();

it != c0.end(); ++it)

std::cout << " " << *it;

std::cout << std::endl;

// display odd elements " 1 3"

std::cout << " " << std::get<1>(c0);

std::cout << " " << std::get<3>(c0);

std::cout << std::endl;

return (0);

}

结果:

0 1 2 3

1 3

4.swap函数的用法

template

void swap(

array& left,

array& right);

交换两个数组的值,示例

#include

#include

typedef std::array Myarray;

int main()

{

Myarray c0 = {0, 1, 2, 3};

// display contents " 0 1 2 3"

for (Myarray::const_iterator it = c0.begin();

it != c0.end(); ++it)

std::cout << " " << *it;

std::cout << std::endl;

Myarray c1 = {4, 5, 6, 7};

c0.swap(c1);

// display swapped contents " 4 5 6 7"

for (Myarray::const_iterator it = c0.begin();

it != c0.end(); ++it)

std::cout << " " << *it;

std::cout << std::endl;

swap(c0, c1);

// display swapped contents " 0 1 2 3"

for (Myarray::const_iterator it = c0.begin();

it != c0.end(); ++it)

std::cout << " " << *it;

std::cout << std::endl;

return (0);

}

结果:

0 1 2 3

4 5 6 7

0 1 2 3

���;��[�.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,841评论 5 472
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,415评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,904评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,051评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,055评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,255评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,729评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,377评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,517评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,420评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,467评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,144评论 3 317
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,735评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,812评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,029评论 1 256
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,528评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,126评论 2 341

推荐阅读更多精彩内容