C++11 容器Array

array是一個固定大小的順序容器,不能動態改變大小,array內的元素在內存中以嚴格的線性順序存儲
與普通數組聲明存儲空間大小[]的方式是同樣有效的,只是加入了一些成員函數和全局函數[get (array)、operators (array)],以便看成標準容器使用
零大小的array是有效的,可是不能夠被成員函數front、back、data間接引用
array的swap是一個線性操做交換全部的元素,一般是很是低效的
Constructor:
1.template < class T, size_t N > class array;

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

Iteratorsc++

 

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,5> 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 accessless

 

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<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中第一個元素的指針
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;  

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;//打印地址  

 

Modifiersspa

 

fill Fill array with value
swap Swap content

 

fill
std::array<int, 5> 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 5code

swap
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 functionsblog

 

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;
 
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)ci

模板原型以下:

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 );
 
  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
相關文章
相關標籤/搜索