array是一个固定大小的顺序容器,不能动态改变大小,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};  
 
Member functions:

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

  1. STL - 容器 - Array

    Array是C++ 11给STL新增加的容器 ArrayTest.cpp #include <array> #include <algorithm> #include < ...

  2. stout代码分析之九:c++11容器新特性

    stout大量使用了c++11的一些新特性,使用这些特性有利于简化我们的代码,增加代码可读性.以下将对一些容器的新特性做一个总结.主要两方面: 容器的初始化,c++11中再也不用手动insert或者p ...

  3. 序列式容器————array

    目录 介绍 1 构造函数 2 fill() 3 元素的获取 4 size() 5 empty() 6 front() 7 back() 8 get<n> 9 迭代器(待补充) 10 元素的 ...

  4. centos7下安装docker(11容器操作总结)

    这段时间主要是学习了对容器的操作,包括:容器的状态:start,stop,restart,rename,pause,unpause,rm,attach,exec,kill,logs:还学习了对容器的资 ...

  5. 【校招面试 之 C/C++】第33题 C++ 11新特性(四)之STL容器

    C++ 11新增array.forward_list(单链表).unordered_set.unordered_map集中容器.

  6. c++11——改进容器性能

    使用emplace_back就地构造 emplace_back能就地通过参数构造对象,不需要拷贝或者移动内存,相比push_back能更好的避免内存的拷贝和移动,使得容器插入元素的性能得到进一步提升. ...

  7. C++ 顺序容器基础知识总结

    0.前言 本文简单地总结了STL的顺序容器的知识点.文中并不涉及具体的实现技巧,对于细节的东西也没有提及.一来不同的标准库有着不同的实现,二来关于具体实现<STL源码剖析>已经展示得全面细 ...

  8. C++11在时空性能方面的改进

    C++11在时空性能方面的改进 这篇我们聊聊C++11在时间和空间上的改进点: 主要包括以下方面: 新增的高效容器:array.forward_list以及unordered containers: ...

  9. c++11新特性(了解)

    从C++出来到现在已经13年了. Bjarne Stroustrup(C++的创造者)最近评价C++:”感觉像个新的语言“. 事实上,C++11核心已经发生了很重大的变化: . 支持Lambda表达式 ...

随机推荐

  1. 怎样查看class文件的jdk版本号

    1.事先编译好一个class文件.如:TestVersion.class 2.使用UltraEdit或Editplus打开class文件,我这里使用的editplus,如图: 3.打开时Encodin ...

  2. Linux库的创建和使用

    Linux库的概念 库是一种软件组建技术,里面封装了数据和函数,提供给用户程序调用.使用库能够使程序模块化,提高编译速度,实现代码重用,易于升级. Windows系统提供了大量静态链接库(.lib)和 ...

  3. Closing a window

    The obvious way to how to close a window is to click on the x mark on the titlebar. In the next exam ...

  4. MySQL 设置慢查询为200ms

    1:查看当前版本并设置long_query_time为0.2 mysql> select version(); +------------+ | version() | +----------- ...

  5. ideaIU-15.0.2 注册码

    注册时选择 License server ,填 http://idea.lanyus.com ,然后点击 OK

  6. springboot EnableAutoConfiguration

    http://blog.javachen.com/2016/02/19/spring-boot-auto-configuration.html 自动配置 在启动类上使用@EnableAutoConfi ...

  7. 【DB2】db2常用SQL语句集合

    持续更新中 1.修改日志模式为不记录 alter table table_name  activate not logged initially; 2.清空表 alter table t1 activ ...

  8. JavaScript中的闭包(closure)

    闭包的特性 1.函数嵌套函数 2.函数内部可以引用外部的参数和变量 3.参数和变量不会被垃圾回收机制回收  闭包的缺点就是常驻内存,会增大内存使用量,使用不当很容易造成内存泄露,主要用于私有的方法和变 ...

  9. PHP中curl的使用

    cURL 函数 curl_close — 关闭一个cURL会话 curl_copy_handle — 复制一个cURL句柄和它的所有选项 curl_errno — 返回最后一次的错误号 curl_er ...

  10. try语句...

    #include<stdio.h>#include<iostream>using namespace std; int main( ){ try { throw "嗨 ...