C++11 容器Array
与普通数组声明存储空间大小[]的方式是一样有效的,只是加入了一些成员函数和全局函数[get (array)、operators (array)],以便当作标准容器使用
零大小的array是有效的,但是不可以被成员函数front、back、data间接引用
array的swap是一个线性操作交换所有的元素,通常是非常低效的
Constructor:
.template < class T, size_t N > class array; 举例:
array<int,10> iArray={1,2,3,4,5,6,7,8,9,10};
Iterators
begin | Return iterator to beginning |
end | Return iterator to end |
rbegin | Return reverse iterator to reverse beginning |
rend | Return reverse iterator to reverse end |
cbegin | Return const_iterator to beginning |
cend | Return const_iterator to end |
crbegin | Return const_reverse_iterator to reverse beginning |
crend | Return const_reverse_iterator to reverse end |
std::array<int,> arr = { , , , , };
std::cout << "arr contains:";
for ( auto it = arr.cbegin(); it != arr.cend(); ++it )
{
*it = 34; //error can't modify *it
arr.begin(); //point to array first element
arr.front(); //return the first element
arr.back(); //return the end element
std::cout << *it << std::endl;
}
Capacity
empty | Test whether list is empty |
size | Return size |
max_size | Return maximum size |
Element access
operator[] | Access element |
at | Access element |
front | Access first element |
back | Access last element |
data | Get pointer to first data |
注意:使用at程序崩溃时不会显示堆栈信息,尽量使用[]去array的值
back
data//返回指向array中第一个元素的指针
const char* cstr = "Test string";
std::array<char,> charray;
std::memcpy (charray.data(),cstr,);
std::cout << charray.data() << std::endl;//如果是char类型则打印值 Test string
//如果array中保存的是int
cout << iArray.data() << endl;//两者等效,等于打印出第一个元素的地址
cout << &charray << endl; array<string,> sArray={"hello","c++","I"};
for (auto it = sArray.cbegin(); it != sArray.cend(); ++it)
{
cout << *it << '\t';//打印出hello c++ I
}
cout << sArray.data() << endl;//打印地址
Modifiers
fill | Fill array with value |
swap | Swap content |
fill
std::array<int, > arr;
arr.fill();
for (auto it = arr.begin(); it != arr.end(); it++)
{
std::cout << " " << *it;
} arr.assign();
for (auto it = arr.begin(); it != arr.end(); it++)
{
std::cout << " " << *it;
}
OutPut:
34 34 34 34 34 5 5 5 5 5
swap
Global functions
get(array) | Get element (tuple interface) (function template ) |
operators (array) | Global relational operator functions for array |
get(array)//Returns a reference to the Ith element of array arr.
函数原型:
.template <size_t I, class T, size_t N> T& get ( array<T,N>& arr ) noexcept;
.template <size_t I, class T, size_t N> T&& get ( array<T,N>&& arr ) noexcept;
.template <size_t I, class T, size_t N> const T& get ( const array<T,N>& arr ) noexcept;
Output:
first element in myarray: 30
first element in mytuple: 10
operators(array)
模板原型如下: .template <class T, size_T N>
bool operator== ( const array<T,N>& lhs, const array<T,N>& rhs );
.template <class T, size_T N>
bool operator!= ( const array<T,N>& lhs, const array<T,N>& rhs );
.template <class T, size_T N>
bool operator< ( const array<T,N>& lhs, const array<T,N>& rhs );
4template <class T, size_T N>
bool operator> ( const array<T,N>& lhs, const array<T,N>& rhs );
.template <class T, size_T N>
bool operator<= ( const array<T,N>& lhs, const array<T,N>& rhs );
.template <class T, size_T N>
bool operator>= ( const array<T,N>& lhs, const array<T,N>& rhs );
std::array<int,> a = {, , , , };
std::array<int,> b = {, , , , };
std::array<int,> c = {, , , , };
if (a==b) std::cout << "a and b are equal\n";
if (b!=c) std::cout << "b and c are not equal\n";
if (b<c) std::cout << "b is less than c\n";
if (c>b) std::cout << "c is greater than b\n";
if (a<=b) std::cout << "a is less than or equal to b\n";
if (a>=b) std::cout << "a is greater than or equal to b\n";
Output:
a and b are equal
b and c are not equal
b is less than c
c is greater than b
a is less than or equal to b
a is greater than or equal to b
C++11 容器Array的更多相关文章
- STL - 容器 - Array
Array是C++ 11给STL新增加的容器 ArrayTest.cpp #include <array> #include <algorithm> #include < ...
- stout代码分析之九:c++11容器新特性
stout大量使用了c++11的一些新特性,使用这些特性有利于简化我们的代码,增加代码可读性.以下将对一些容器的新特性做一个总结.主要两方面: 容器的初始化,c++11中再也不用手动insert或者p ...
- 序列式容器————array
目录 介绍 1 构造函数 2 fill() 3 元素的获取 4 size() 5 empty() 6 front() 7 back() 8 get<n> 9 迭代器(待补充) 10 元素的 ...
- centos7下安装docker(11容器操作总结)
这段时间主要是学习了对容器的操作,包括:容器的状态:start,stop,restart,rename,pause,unpause,rm,attach,exec,kill,logs:还学习了对容器的资 ...
- 【校招面试 之 C/C++】第33题 C++ 11新特性(四)之STL容器
C++ 11新增array.forward_list(单链表).unordered_set.unordered_map集中容器.
- c++11——改进容器性能
使用emplace_back就地构造 emplace_back能就地通过参数构造对象,不需要拷贝或者移动内存,相比push_back能更好的避免内存的拷贝和移动,使得容器插入元素的性能得到进一步提升. ...
- C++ 顺序容器基础知识总结
0.前言 本文简单地总结了STL的顺序容器的知识点.文中并不涉及具体的实现技巧,对于细节的东西也没有提及.一来不同的标准库有着不同的实现,二来关于具体实现<STL源码剖析>已经展示得全面细 ...
- C++11在时空性能方面的改进
C++11在时空性能方面的改进 这篇我们聊聊C++11在时间和空间上的改进点: 主要包括以下方面: 新增的高效容器:array.forward_list以及unordered containers: ...
- c++11新特性(了解)
从C++出来到现在已经13年了. Bjarne Stroustrup(C++的创造者)最近评价C++:”感觉像个新的语言“. 事实上,C++11核心已经发生了很重大的变化: . 支持Lambda表达式 ...
随机推荐
- javaweb笔记分享
Lesson 1 一.eclipse工具的使用 1. java代码的位置 1) 选择工作空间 workspace 选择一个文件夹存放程序(代码) 不要用中文和空格 2) 新建一个java 工程(Pr ...
- EXCEL 列与列怎么交换?
选中A列数据,按先SHIFT键的同时按住鼠标左键,向右拖动鼠标,在拖动的过程中,会出现一条虚线,当拖动到B列的右边缘时,屏幕上会出现 C:C 的提示,这时送开SHIFT键及鼠标左键,就完成了A B两列 ...
- 【FinacialKnowledge】财务报表及名词解释
1.财务报表 以下三张表为:资产负债表.利润表.现金流量表 ...
- 使用poi导出execl
使用poi需要用到的jar包 本文的导出基于execl的模板导出,在大部分表头固定而格式花样比较复杂的建议使用本文介绍的方法(表头固定,只需要填充值) 1.在webroot目录下新建report文件夹 ...
- CAS 5.1.x 的搭建和使用(四)—— 配置使用HTTP协议访问的服务端
CAS单点登录系列: CAS 5.1.x 的搭建和使用(一)—— 通过Overlay搭建服务端 CAS5.1.x 的搭建和使用(二)—— 通过Overlay搭建服务端-其它配置说明 CAS5.1.x ...
- Linux服务器修改时区时间
时间的一致性很关键,对于日志的分析和程序的对接都至关重要! 01.tzselect 修改时区 可以使用命令 tzselect,修改时区.操作示例: $ tzselect Please identify ...
- oracle默认连接数150
当oracle连接数超过最大的150登录不上 sqlplus / as sysdba ###报错,提示连接数未释放! ---释放网络连接数 ps -ef |grep ora |grep " ...
- esp8266烧录Html文件,实现内置网页控制设备!
代码地址如下:http://www.demodashi.com/demo/14321.html 一.前言: 这个月也快结束了,时间真快,我服务器知识自学依然在路途中,这几天听到热点网页配置esp826 ...
- iOS- 非整星的评分控件(支持小数)
概述 订单评论里实现星级评分控件: 简单整星评价与非整星的精评价. 详细 代码下载:http://www.demodashi.com/demo/10711.html 现在很多应用都有评分功能. 有了订 ...
- swift上传图片
import UIKit import AFNetworking class YJRequest: NSObject { //#pragma mark - 上传图片 func uploadImageW ...