博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++11 容器Array
阅读量:6985 次
发布时间:2019-06-27

本文共 5198 字,大约阅读时间需要 17 分钟。

array是一个固定大小的顺序容器,不能动态改变大小,array内的元素在内存中以严格的线性顺序存储
与普通数组声明存储空间大小[]的方式是一样有效的,只是加入了一些成员函数和全局函数[get (array)、operators (array)],以便当作标准容器使用
零大小的array是有效的,但是不可以被成员函数front、back、data间接引用
array的swap是一个线性操作交换所有的元素,通常是非常低效的
Constructor:1.template < class T, size_t N > class array; 举例: array
 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
arr = { 2, 16, 77, 34, 50 }; 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
std::array
myarray = {
5, 19, 77}; std::cout << "front is: " << myarray.front() << std::endl; // 5 std::cout << "back is: " << myarray.back() << std::endl; // 77 myarray.back() = 50; for ( int& x : myarray )   std::cout << " " << x; //5 19 50

 

data//返回指向array中第一个元素的指针
const char* cstr = "Test string";  std::array
charray; std::memcpy (charray.data(),cstr,12); std::cout << charray.data() << std::endl;//如果是char类型则打印值 Test string //如果array中保存的是int cout << iArray.data() << endl;//两者等效,等于打印出第一个元素的地址 cout << &charray << endl; array
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
arr;arr.fill(34);for (auto it = arr.begin(); it != arr.end(); it++){ std::cout << " " << *it;}arr.assign(5);for (auto it = arr.begin(); it != arr.end(); it++){ std::cout << " " << *it;}

OutPut:  

 34 34 34 34 34 5 5 5 5 5

swap
std::array
first = {
10, 20, 30, 40, 50}; std::array
second = {
11, 22, 33, 44, 55}; first.swap (second); std::cout << "first:"; for (int& x : first) std::cout << " " << x;

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.

函数原型:1.template 
T& get ( array
& arr ) noexcept;2.template
T&& get ( array
&& arr ) noexcept;3.template
const T& get ( const array
& arr ) noexcept;
 
std::array
myarray = {
10, 20, 30}; std::tuple
mytuple (10, 20, 30); std::tuple_element<0,decltype(myarray)>::type myelement; // int [decltype是新标准中用来取类型] //array头文件中重载了tuple_element和tuple_size方便和tuple交互 //交换myarray[0]和myarray[2] myelement = std::get<2>(myarray);//取出array中的30 std::get<2>(myarray) = std::get<0>(myarray);//把array中的10换成30 std::get<0>(myarray) = myelement;//把30赋值给第一个元素 std::cout << "first element in myarray: " << std::get<0>(myarray) << "\n"; std::cout << "first element in mytuple: " << std::get<0>(mytuple) << "\n";
Output:
first element in myarray: 30
first element in mytuple: 10

operators(array)

模板原型如下:1.template 
bool operator== ( const array
& lhs, const array
& rhs );2.template
bool operator!= ( const array
& lhs, const array
& rhs );3.template
bool operator< ( const array
& lhs, const array
& rhs );4template
bool operator> ( const array
& lhs, const array
& rhs );5.template
bool operator<= ( const array
& lhs, const array
& rhs );6.template
bool operator>= ( const array
& lhs, const array
& rhs );
 
std::array
a = {
10, 20, 30, 40, 50}; std::array
b = {
10, 20, 30, 40, 50}; std::array
c = {
50, 40, 30, 20, 10}; 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
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
 

转载于:https://www.cnblogs.com/DswCnblog/p/5671799.html

你可能感兴趣的文章
android自定义radiobutton样式文字颜色随选中状态而改变
查看>>
【CodeForces 604B】F - 一般水的题1-More Cowbe
查看>>
eclipse调试java程序的九个技巧
查看>>
用JS获取地址栏参数的方法
查看>>
巴特沃斯(Butterworth)滤波器 (2) - 双线性变换
查看>>
iOS 小知识点(持续更新)
查看>>
iOS人脸识别(CoreImage)
查看>>
java.net.SocketException: Software caused connection abort: socket write error
查看>>
MySQL 随机取数据效率问题
查看>>
ArcGIS for Desktop入门教程_第八章_Desktop学习资源 - ArcGIS知乎-新一代ArcGIS问答社区...
查看>>
根据id查询数据(向前台返回json格式的数据)
查看>>
JMeter中3种参数值的传递
查看>>
hive操作语句使用详解
查看>>
根据指定类型计算两个日期相差的时间
查看>>
【转】【Linux】linux awk命令详解
查看>>
EasyUI ---- draggable购物车
查看>>
Jdom读取XML文件
查看>>
Spring Boot 配置文件 – 在坑中实践
查看>>
研究技术心得
查看>>
在windows搭建jenkins測试环境
查看>>