电子商务网站建设交印花税吗,四年级说新闻2023,设置网站建设方案,网站图片做多大文章目录测试环境array介绍头文件模块类定义对象构造初始化元素访问容器大小迭代器其他函数测试环境
系统#xff1a;ubuntu 22.04.2 LTS 64位 gcc版本#xff1a;11.3.0 编辑器#xff1a;vsCode 1.76.2
array介绍
array是固定大小的序列式容器#xff0c;它包含按严格…
文章目录测试环境array介绍头文件模块类定义对象构造初始化元素访问容器大小迭代器其他函数测试环境
系统ubuntu 22.04.2 LTS 64位 gcc版本11.3.0 编辑器vsCode 1.76.2
array介绍
array是固定大小的序列式容器它包含按严格的线性序列排列的特定数量的元素。与其他容器不同array具有固定的大小并且不通过分配器管理其元素的分配因此其不能动态的扩展和收缩。零大小的数组是有效的。元素存储在连续的内存位置中大小是编译时常数没有内存或时间开销
头文件
#include array模块类定义
_Tp表示自定义数据类型 _Nm表示_Ty的数量
template class Tp, std::size_t _Nm class array;对象构造
/* 默认构造函数构造一个包含10个int类型的数组对象 */
std:arrayint, 10 arrInt;
/* (拷贝构造函数)使用arrInt构造一个相同的数组对象 */
std::arrayint, 10 arrInt1(arrInt)
/* 构造0长数组 */
std::arrayint, 0 arrZero;
/* 二维数组构造,多维数组同理 */
std::arraystd::arrayint, 10, 10 arrInt2;初始化
/* 使用初始化列表进行初始化 */
arrInt {0,1,2,3,4,5,6,7,8,9};元素访问
函数名返回值功能[]指定元素的值访问元素at()指定元素的引用访问元素front()首元素的引用获取首元素back()元素的引用获取末尾元素data()首元素的地址获取指向首元素的指针
/* 使用下标运算符访问元素并赋值 */
for (int i 0; i 10; i)
{std::cout arrInt[i] ;
}
/* 使用at访问元素并赋值 */
for (int i 0; i 10; i)
{std::cout arrInt.at(i) ;
}
/* 取数组第一个和最后一个元素返回值类型为元素的引用注0长度数组不能使用运行时后出错 */
std::cout arrInt.front() arrInt.back() std::endl;
/* 获取指向数组头的指针 */
int *p arrInt.data();注front()和back()不能用于获取0长度数组的元素。
容器大小
函数返回值功能empty()bool判断array是否为空为空返回true不为空返回falsesize()std:size_t获取数组长度max_size()std::size_t获取数组的最大长度与size()相同
/* 判断数组是否为空, 返回值为bool类型数组为空返回true否则返回false只有0长度数组返回true */
std::cout arrInt.empty() std::endl;
/* 获取数组大小,注max_size在array中获取的大小与size相同其他容器中可能大小不一致*/
std::cout arrInt.size() arrInt.max_size() std::endl;注empty()只有0长度数组才会返回truesize()和max_size()获取的大小始终相同为数组长度。
迭代器
类型功能iterator正向随机访问迭代器。从前向后访问元素可以读取也可以修改const_iterator常量正向随机访问迭代器。从前向后访问元素只能读取不能修改reverse_iterator逆向随机访问迭代器。从后向前访问元素可以读取也可以修改const_reverse_iterator常量逆向随机访问迭代器。从后向前访问元素只能读取不能修改
函数返回值功能begin()正向随机访问迭代器返回指向array对象首元素所在位置的迭代器end()正向随机访问迭代器返回指向array对象末尾元素的下一个位置的迭代器cbegin()常量正向随机访问迭代器返回指向array对象首元素所在位置的常量迭代器cend()常量正向随机访问迭代器返回指向array对象末尾元素的下一个位置的迭代器rbegin()逆向随机访问迭代器返回指向array对象末尾元素位置的迭代器rend()逆向随机访问迭代器返回指向array对象首元素的前一个位置的迭代器crbegin()常量逆向随机访问迭代器返回指向array对象末尾元素位置的常量迭代器crend()常量逆向随机访问迭代器返回指向array对象首元素的前一个位置的常量迭代器
/*正向随机访问迭代器每个元素10并打印输出10 11 12 13 14 15 16 17 18 19*/
std::arrayint, 10::iterator itr;
for (itr arrInt.begin(); itr ! arrInt.end(); itr)
{/* 修改元素值每个元素10 */*itr 10; /* 访问元素 */std::cout *itr ;
}/*常量正向随机访问迭代器并打印输出10 11 12 13 14 15 16 17 18 19*/
std::arrayint, 10::const_iterator cItr;
for (cItr arrInt.begin(); cItr ! arrInt.end(); cItr)
{/* 不允许修改值编译报错 *///*cItr 10; /* 访问元素 */std::cout *cItr ;
}/*逆向随机访问迭代器每个元素100并打印输出119 118 117 116 115 114 113 112 111 100*/
std::arrayint, 10::reverse_iterator rItr;
for (rItr arrInt.rbegin(); rItr! arrInt.rend(); rItr)
{/* 修改元素值 */*rItr 100; /* 访问元素 */std::cout *rItr ;
}/*常量逆向随机访问迭代器并打印输出119 118 117 116 115 114 113 112 111 100*/
std::arrayint, 10::const_reverse_iterator crItr;
for (crItr arrInt.crbegin(); crItr! arrInt.crend(); crItr)
{/* 不允许修改元素值 编译报错 *///*crItr 100; /* 访问元素 */std::cout *crItr ;
}其他函数
函数返回值功能fill()无将array中的所有元素用指定的元素进行填充swap()无交换两个容器的元素
/* 数据填充,将数组所有元素填充为15,无返回值 */
arrInt.fill(15);
/*交互两个数组元素的值,无返回值*/
std::arrayint, 5 arrTest1;
std::arrayint, 5 arrTest2:
arrTest1.fill(10);
arrTest2.fill(15);
arrTest1.swap(arrTest2);注empty()只有0长度数组才会返回truefront()和back()不能用于获取0长度数组的元素。size()和max_size()获取的大小始终相同为数组长度。 文章转载自: http://www.morning.dysgr.cn.gov.cn.dysgr.cn http://www.morning.djxnn.cn.gov.cn.djxnn.cn http://www.morning.zqybs.cn.gov.cn.zqybs.cn http://www.morning.lxcwh.cn.gov.cn.lxcwh.cn http://www.morning.qxwwg.cn.gov.cn.qxwwg.cn http://www.morning.tfei69.cn.gov.cn.tfei69.cn http://www.morning.xdpjf.cn.gov.cn.xdpjf.cn http://www.morning.klyyd.cn.gov.cn.klyyd.cn http://www.morning.rshs.cn.gov.cn.rshs.cn http://www.morning.srrrz.cn.gov.cn.srrrz.cn http://www.morning.nqnqz.cn.gov.cn.nqnqz.cn http://www.morning.zdmrf.cn.gov.cn.zdmrf.cn http://www.morning.gllhx.cn.gov.cn.gllhx.cn http://www.morning.hhfwj.cn.gov.cn.hhfwj.cn http://www.morning.zcfmb.cn.gov.cn.zcfmb.cn http://www.morning.lrnfn.cn.gov.cn.lrnfn.cn http://www.morning.wzyfk.cn.gov.cn.wzyfk.cn http://www.morning.ngqdp.cn.gov.cn.ngqdp.cn http://www.morning.ccyns.cn.gov.cn.ccyns.cn http://www.morning.tlfyb.cn.gov.cn.tlfyb.cn http://www.morning.hrydl.cn.gov.cn.hrydl.cn http://www.morning.ljbch.cn.gov.cn.ljbch.cn http://www.morning.cplym.cn.gov.cn.cplym.cn http://www.morning.kcbml.cn.gov.cn.kcbml.cn http://www.morning.dpflt.cn.gov.cn.dpflt.cn http://www.morning.bntfy.cn.gov.cn.bntfy.cn http://www.morning.tgxrm.cn.gov.cn.tgxrm.cn http://www.morning.hilmwmu.cn.gov.cn.hilmwmu.cn http://www.morning.nclbk.cn.gov.cn.nclbk.cn http://www.morning.cyyhy.cn.gov.cn.cyyhy.cn http://www.morning.lkbyj.cn.gov.cn.lkbyj.cn http://www.morning.ljdd.cn.gov.cn.ljdd.cn http://www.morning.qhjkz.cn.gov.cn.qhjkz.cn http://www.morning.wjwfj.cn.gov.cn.wjwfj.cn http://www.morning.qkdbz.cn.gov.cn.qkdbz.cn http://www.morning.jklns.cn.gov.cn.jklns.cn http://www.morning.bsqth.cn.gov.cn.bsqth.cn http://www.morning.yqqxj1.cn.gov.cn.yqqxj1.cn http://www.morning.tnjz.cn.gov.cn.tnjz.cn http://www.morning.ldqzz.cn.gov.cn.ldqzz.cn http://www.morning.jjpk.cn.gov.cn.jjpk.cn http://www.morning.qrmyd.cn.gov.cn.qrmyd.cn http://www.morning.mnjwj.cn.gov.cn.mnjwj.cn http://www.morning.fqklt.cn.gov.cn.fqklt.cn http://www.morning.rqkzh.cn.gov.cn.rqkzh.cn http://www.morning.wcjk.cn.gov.cn.wcjk.cn http://www.morning.jbxmb.cn.gov.cn.jbxmb.cn http://www.morning.qgdsd.cn.gov.cn.qgdsd.cn http://www.morning.fdmfn.cn.gov.cn.fdmfn.cn http://www.morning.lmctj.cn.gov.cn.lmctj.cn http://www.morning.dqrpz.cn.gov.cn.dqrpz.cn http://www.morning.nbnq.cn.gov.cn.nbnq.cn http://www.morning.nkyqh.cn.gov.cn.nkyqh.cn http://www.morning.pmsl.cn.gov.cn.pmsl.cn http://www.morning.schwr.cn.gov.cn.schwr.cn http://www.morning.wjmb.cn.gov.cn.wjmb.cn http://www.morning.tnwwl.cn.gov.cn.tnwwl.cn http://www.morning.nmymn.cn.gov.cn.nmymn.cn http://www.morning.cwgn.cn.gov.cn.cwgn.cn http://www.morning.znrlg.cn.gov.cn.znrlg.cn http://www.morning.xlclj.cn.gov.cn.xlclj.cn http://www.morning.wbxbj.cn.gov.cn.wbxbj.cn http://www.morning.bbxbh.cn.gov.cn.bbxbh.cn http://www.morning.rkqzx.cn.gov.cn.rkqzx.cn http://www.morning.dcpbk.cn.gov.cn.dcpbk.cn http://www.morning.gassnw.com.gov.cn.gassnw.com http://www.morning.zrpbf.cn.gov.cn.zrpbf.cn http://www.morning.kpgbz.cn.gov.cn.kpgbz.cn http://www.morning.aiai201.cn.gov.cn.aiai201.cn http://www.morning.jsljr.cn.gov.cn.jsljr.cn http://www.morning.lwnb.cn.gov.cn.lwnb.cn http://www.morning.ljllt.cn.gov.cn.ljllt.cn http://www.morning.jrqw.cn.gov.cn.jrqw.cn http://www.morning.kzxlc.cn.gov.cn.kzxlc.cn http://www.morning.bhmnp.cn.gov.cn.bhmnp.cn http://www.morning.tmcmj.cn.gov.cn.tmcmj.cn http://www.morning.yzzfl.cn.gov.cn.yzzfl.cn http://www.morning.rqhbt.cn.gov.cn.rqhbt.cn http://www.morning.shprz.cn.gov.cn.shprz.cn http://www.morning.cmrfl.cn.gov.cn.cmrfl.cn