Chapter 10.顺序容器array[c++11]

array[c++11]简介

array是一个固定大小的顺序容器,不能动态改变大小,array内的元素在内存中以严格的线性顺序存储
与普通数组声明存储空间大小[]的方式是一样有效的,只是加入了一些成员函数和全局函数[get (array)、operators (array)],以便当作标准容器使用
零大小的array是有效的,但是不可以被成员函数front、back、data间接引用
array的swap是一个线性操作交换所有的元素,通常是非常低效的

Constructor:

1.template < class T, size_t N > class array;

eg:
array<int,10> iArray={1,2,3,4,5,6,7,8,9,10};

Member functions:

Iterators

beginReturn iterator to beginning
endReturn iterator to end
rbeginReturn reverse iterator to reverse beginning
rendReturn reverse iterator to reverse end
cbeginReturn const_iterator to beginning
cendReturn const_iterator to end
crbeginReturn const_reverse_iterator to reverse beginning
crendReturn const_reverse_iterator to reverse end

eg:
  std::array<int,5> myarray = { 2, 16, 77, 34, 50 };
  std::cout << "myarray contains:";
  for ( auto it = myarray.cbegin(); it != myarray.cend(); ++it )
    std::cout << " " << *it;
eg:
  std::array<int,6> myarray = {10, 20, 30, 40, 50, 60} ;
  std::cout << "myarray contains:";
  for ( auto rit=myarray.crbegin() ; rit < myarray.crend(); ++rit )
    std::cout << " " << *rit;   // cannot modify *rit

Capacity

emptyTest whether list is empty
sizeReturn size
max_sizeReturn maximum size

Element access

operator[]Access element
atAccess element
frontAccess first element
backAccess last element
dataGet pointer to data

operator[]
eg:
  std::array<int,10> myarray;
  unsigned int i;
  // assign some values:
  for (i=0; i<10; i++) myarray[i]=i;
  // print content
  std::cout << "myarray contains:";
  for (i=0; i<10; i++)
    std::cout << " " << myarray[i];
back
eg:
  std::array<int,3> 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中第一个元素的指针
eg:
  const char* cstr = "Test string";
  std::array<char,12> 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;
eg:
	array<string,5> sArray={"hello","c++","I"};
	for (auto it = sArray.cbegin(); it != sArray.cend(); ++it)
	{
		cout << *it << '\t';//打印出hello	c++	I
	}
	cout << sArray.data() << endl;//打印地址

Modifiers

fillFill array with value
swapSwap content

fill
eg:
  std::array<int,6> myarray;
  myarray.fill(5);
  for ( int& x : myarray) { std::cout << " " << x; }
OutPut:
5 5 5 5 5 5
swap
eg:
  std::array<int,5> first = {10, 20, 30, 40, 50};
  std::array<int,5> 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 <size_t I, class T, size_t N> T& get ( array<T,N>& arr ) noexcept;
2.template <size_t I, class T, size_t N> T&& get ( array<T,N>&& arr ) noexcept;
3.template <size_t I, class T, size_t N> const T& get ( const array<T,N>& arr ) noexcept;

eg:
  std::array<int,3> myarray = {10, 20, 30};
  std::tuple<int,int,int> 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 <class T, size_T N>
    bool operator== ( const array<T,N>& lhs, const array<T,N>& rhs );
2.template <class T, size_T N>
    bool operator!= ( const array<T,N>& lhs, const array<T,N>& rhs );
3.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 );
5.template <class T, size_T N>
    bool operator<= ( const array<T,N>& lhs, const array<T,N>& rhs );
6.template <class T, size_T N>
    bool operator>= ( const array<T,N>& lhs, const array<T,N>& rhs );

eg:
  std::array<int,5> a = {10, 20, 30, 40, 50};
  std::array<int,5> b = {10, 20, 30, 40, 50};
  std::array<int,5> 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<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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值