#include <array>
array是静态数组,在栈上,不可以变长
vector比array更常用
不需要变长,容量较小,用array
需要变长,容量较大,用vector
1 array
1 array新数组
//std::array<数组元素类型, 数组列数> 一维数组变量名
std::array<double, 4> dbnew1 = { 10.1,10.2,10.3,10.4 };//新数组
std::array<double, 4> dbnew2 = dbnew1;//新版数组可以实现整体赋值,适合于操作对象数组
#include <iostream>
#include <array>
using namespace std; void main()
{
double db[] = { 1.1,2.2,3.3,4.4 };//旧数组 //std::array<数组元素类型, 数组元素个数> 一维数组变量名
std::array<double, > dbnew1 = { 10.1,10.2,10.3,10.4 };//新数组 std::array<double, > dbnew2 = dbnew1;//新版数组可以实现整体赋值,适合于操作对象数组 for (int i = ; i < ; i++)//打印
{
std::cout << db[i] << " ";
}
std::cout << std::endl; for (int i = ; i < ; i++)//打印
{
std::cout << dbnew1[i] << " ";
}
std::cout << std::endl; for (int i = ; i < ; i++)//打印
{
std::cout << dbnew2[i] << " ";
} system("pause");
}
使用C++风格数组不需要管理内存
array注意不要栈溢出
array适用于任何类型
array二维数组,三维数组
//std::array<数组元素类型, 数组列数> 一维数组变量名
//std::array<std::array<数组元素类型, 数组行数>, 数组列数> 二维数组变量名
#include <iostream>
#include <array>
using namespace std; void main()
{
//std::array<数组元素类型, 数组列数> 一维数组变量名
std::array<int, >myint1 = { ,,,, };//一维数组
std::array<int, >myint2 = { ,,,, };//一维数组
std::array<int, >myint3 = { ,,,, };//一维数组 //std::array<std::array<数组元素类型, 数组行数>, 数组列数> 二维数组变量名
std::array<std::array<int, >, >myint = { myint1,myint2,myint3 };//内嵌数组,二维数组
std::array<std::array<int, >, >myinta = { ,,,,,,,,,,,,,, };//二维数组 std::array<std::array<std::array<int, >, >, >myintb = { };//三维数组 for (int i = ; i < ; i++)//打印二维数组打印
{
for (int j = ; j < ; j++)
{
std::cout << " " << myint[i][j];
}
std::cout << std::endl;
}
std::cout << std::endl; for (int i = ; i < ; i++)//打印二维数组打印
{
for (int j = ; j < ; j++)
{
std::cout << " " << myinta[i][j];
}
std::cout << std::endl;
}
std::cout << std::endl; for (int i = ; i < ; i++)//打印三维数组
{
for (int j = ; j < ; j++)
{
for (int k = ; k < ; k++)
{
std::cout << " " << myintb[i][j][k];
}
std::cout << std::endl;
}
std::cout << std::endl;
} system("pause");
}
迭代器循环遍历数组
从头到尾iterator
从尾到头reverse_iterator
#include <iostream>
#include <array>
using namespace std; void main()
{
array<int, > myint = { ,,,, }; array<int, >::iterator ibegin, iend;//迭代器
ibegin = myint.begin();//数据起始点
iend = myint.end();//结束 for (; ibegin != iend; ibegin++)//从头到尾
{
cout << *ibegin << endl;
} array<int, >::reverse_iterator rbegin, rend;//迭代器
rbegin = myint.rbegin();//数据起始点
rend = myint.rend();//结束 while (rbegin != rend)//从尾到头
{
cout << *rbegin << endl;
rbegin++;
} system("pause");
}
//当构造函数带有参数的情况下,如果要建立类的数组,这时候必须要用C++风格数组
#include <iostream>
#include <array> class classobj
{
public:
int num;
explicit classobj(int data)//构造函数需要参数
{
this->num = data;
std::cout << "被构造" << num << std::endl;
}
~classobj()
{
std::cout << "被销毁" << num << std::endl;
}
}; void run()
{
classobj obj();//创建对象必须有合适的构造函数 classobj *p = new classobj();//创建指针 std::array <classobj, >myarray = { obj,*p };//当构造函数带有参数的情况下,如果要建立类的数组,这时候必须要用C++风格数组
} void main()
{
run(); system("pause");
}
#include <array>的更多相关文章
- 浅谈JSP中include指令与include动作标识的区别
JSP中主要包含三大指令,分别是page,include,taglib.本篇主要提及include指令. include指令使用格式:<%@ include file="文件的绝对路径 ...
- Entity Framework 6 Recipes 2nd Edition(13-9)译 -> 避免Include
问题 你想不用Include()方法,立即加载一下相关的集合,并想通过EF的CodeFirst方式实现. 解决方案 假设你有一个如Figure 13-14所示的模型: Figure 13-14. A ...
- error RC1015: cannot open include file 'afxres.h' 解决办法
在为WindowsPhone8程序添加本地化的过程中遇到这个问题: 问题原因就是afxres.h文件缺失,下载它,放到VS安装目录下的VS\include目录下就可以了(选择目录的时候注意对应对版本) ...
- Mybatis常用总结:参数,返回,执行sql,include等
1.参数注入1.1用#{0},#{1}的形式,0代表第一个参数,1代表第二个参数 public List<RecordVo> queryList(String workerId, Inte ...
- jsp中的@include与jsp:include区别详解
1 前言 搞java开发的人也许都知道在jsp中引入项目中其他文件有如下两种方式 <%@include file="xxx.jsp"%> <jsp:include ...
- JSP中编译指令include与动作指令include的区别
include指令是编译阶段的指令,即include所包含的文件的内容是编译的时候插入到JSP文件中,JSP引擎在判断JSP页面未被修改, 否则视为已被修改.由于被包含的文件是在编译时才插入的,因此如 ...
- C/C++ 中的include
当需要使用已有的方法或库时, 可以将它们的头文件#include进来. #include会在preprocess过程中被替换成它包含的代码. 头文件中包含了需要使用的函数/变量的声明. 当然声明与定义 ...
- 织梦多语言站点,{dede:include filename=''/}引入问题
织梦模板include插入非模板目录文件出现"无法在这个位置找到"错误的解决办法 以下是dede V55_UTF8 查dede include标签手册 (3) include 引入 ...
- PHP 站点相对包含,路径的问题解决方法(include,require)
以前看了,很多框架,基本上很少使用相对路径包含.而一般很多做php web站点,喜欢用相对路径. 认为这样,无论目录放到那里. 只要跟另外目录关系一致.那么就不会出现问题.如果一个站点,一般都认为,如 ...
- 如何让include标签包裹的布局置于屏幕最下方?
如何让一个Layout 始终在屏幕的下方 我想让<include layout="@layout/bottom" />一直在屏幕下,怎么做? 1.相对布局中用属性 a ...
随机推荐
- network-manager与interfaces冲突
网络配置的两种方式 Ubuntu下修改网络配置有两种方式:图形界面方式(network-manager)和修改/etc/network/interfaces 但是如果两种方式的网络设置不同,就会产生冲 ...
- 安卓使用spinner控件和pull解析实现全国省市县的三级联动
实现该功能主要有两个难点: 1.XML文件的Pull解析.由于XML文件的结构较为复杂,所以解析时要注意,划分XML文件的结构,根据结构建立相应的和集合.本例中需要建立三个类和三个对象.三个类分别为, ...
- Web学习
http://book.2cto.com/201309/31936.html http://alvinalexander.com/ 查看锁表进程SQL语句1: select sess.sid, ...
- 鼠标拖放div 实现
Javascript的mousemove事件类型是一个实时响应的事件,当鼠标指针的位置发生变化时(至少移动1个像素),就会触发mousemove事件.该事件响应的灵敏度主要参考鼠标指针移动速度的快慢, ...
- 微信cookie内容
#LWP-Cookies-1.0 Set-Cookie3: webwx_data_ticket="AQeVHpn/pdyrAQHCl++4ZvS0"; path="/&q ...
- C语言入门(19)——C语言的编码风格
代码风格好不好就像字写得好不好看一样,如果一个公司招聘秘书,肯定不要字写得难看的,同理,代码风格糟糕的程序员肯定也是不称职的.虽然编译器不会挑剔难看的代码,照样能编译通过,但是和你一个团队进行协作的其 ...
- SP_CreateInsertScript 将表内的数据全部拼接成INSERT字符串输出
),)) as begin set nocount on ) ) ) select @sqlstr='select ''insert '+@tablename select @sqlstr1='' s ...
- Unity3d 打包时报错 CommandInvokationFailure: Unable to convert classes into dex format. See the Console for details.
今天打包带有Android插件的unity3d 项目是,报错CommandInvokationFailure: Unable to convert classes into dex format. S ...
- linear encoder 和 PCA
- skiing(搜索+记忆化搜索)
skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...