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>的更多相关文章

  1. 浅谈JSP中include指令与include动作标识的区别

    JSP中主要包含三大指令,分别是page,include,taglib.本篇主要提及include指令. include指令使用格式:<%@ include file="文件的绝对路径 ...

  2. Entity Framework 6 Recipes 2nd Edition(13-9)译 -> 避免Include

    问题 你想不用Include()方法,立即加载一下相关的集合,并想通过EF的CodeFirst方式实现. 解决方案 假设你有一个如Figure 13-14所示的模型: Figure 13-14. A ...

  3. error RC1015: cannot open include file 'afxres.h' 解决办法

    在为WindowsPhone8程序添加本地化的过程中遇到这个问题: 问题原因就是afxres.h文件缺失,下载它,放到VS安装目录下的VS\include目录下就可以了(选择目录的时候注意对应对版本) ...

  4. Mybatis常用总结:参数,返回,执行sql,include等

    1.参数注入1.1用#{0},#{1}的形式,0代表第一个参数,1代表第二个参数 public List<RecordVo> queryList(String workerId, Inte ...

  5. jsp中的@include与jsp:include区别详解

    1 前言 搞java开发的人也许都知道在jsp中引入项目中其他文件有如下两种方式 <%@include file="xxx.jsp"%> <jsp:include ...

  6. JSP中编译指令include与动作指令include的区别

    include指令是编译阶段的指令,即include所包含的文件的内容是编译的时候插入到JSP文件中,JSP引擎在判断JSP页面未被修改, 否则视为已被修改.由于被包含的文件是在编译时才插入的,因此如 ...

  7. C/C++ 中的include

    当需要使用已有的方法或库时, 可以将它们的头文件#include进来. #include会在preprocess过程中被替换成它包含的代码. 头文件中包含了需要使用的函数/变量的声明. 当然声明与定义 ...

  8. 织梦多语言站点,{dede:include filename=''/}引入问题

    织梦模板include插入非模板目录文件出现"无法在这个位置找到"错误的解决办法 以下是dede V55_UTF8 查dede include标签手册 (3) include 引入 ...

  9. PHP 站点相对包含,路径的问题解决方法(include,require)

    以前看了,很多框架,基本上很少使用相对路径包含.而一般很多做php web站点,喜欢用相对路径. 认为这样,无论目录放到那里. 只要跟另外目录关系一致.那么就不会出现问题.如果一个站点,一般都认为,如 ...

  10. 如何让include标签包裹的布局置于屏幕最下方?

    如何让一个Layout 始终在屏幕的下方 我想让<include layout="@layout/bottom" />一直在屏幕下,怎么做? 1.相对布局中用属性  a ...

随机推荐

  1. 老司机带你用vagrant打造一站式python开发测试环境

      前言 作为一个学习和使用Python的老司机,好像应该经常总结一点东西的,让新司机尽快上路,少走弯路,然后大家一起愉快的玩耍. 今天,咱们就使用vagrant配合xshell打造一站式Python ...

  2. linux下小记

    今天碰到一个问题 记录下 /usr/bin/ld: cannot find ld 和ldconfig的区别 使用makefile编译的时候提示ld提示某个so找不到 当时使用ldconfig查了下 发 ...

  3. Android设置全屏

    全屏显示 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLA ...

  4. windows程序设计读书笔记3——字符显示2

    由于显示的字符可能会不全,我们很容易想到的一个解决办法是使用滚动条. 先看一下代码,再进行分析: /*------------------------------------------------- ...

  5. poj3589---判断两个数有多接近

    #include <stdio.h> #include <stdlib.h> int main() { ],s2[]; int a,b,i,j,n; scanf("% ...

  6. 全国计算机等级考试二级教程-C语言程序设计_第16章_文件

    写入一段文本到文件 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> main() { ...

  7. node配置运行环境变量;

    node express 在开发环境和生产环境运行的代码是不一样的, 通常是先配置好的,在开发环境运行一套代码,在生产环境运行另一套代码, 开发环境 development, 生产环境producti ...

  8. Android开发8:UI组件TextView,EditText,Button

    版本:Android4.3 API18 学习整理:liuxinming TextView 概述 TextView直接继承了View(EditText.Button两个UI组件类的父类) TextVie ...

  9. SQLite语法

    一.建立数据库 sqlite3.exe test.db 二.双击sqlite-3_6_16目录下的程序sqlite3.exe,即可运行 三.退出 .exit 或者 .quit 四.SQLite支持如下 ...

  10. 使用关联对象(AssociatedObject)为UIButton添加Block响应

    在开发中,要给UIButton添加点击事件的话,通常的做法是这样的 UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [ ...