@zhengyuhong
2015-06-09T09:58:46.000000Z
字数 1414
阅读 1075
C++11 STL
template < class T, size_t N > class array;
std::array是一个支持随机访问且大小(size)固定的容器(译注:可以认为是一个紧缩版的vector吧)。它有如下特点:
不预留多余空间,只分配必须空间(译注:size() == capacity())。
可以使用初始化表(initializer list)的方式进行初始化。
保存了自己的size信息。
array<int,6> a = { 1, 2, 3 };a[3]=4;int x = a[5]; // array的默认数据元素为0,所以x的值变成0int* p1 = a; // 错误: std::array不能隐式地转换为指针int* p2 = a.data(); // 正确,data()得到指向第一个元素的指针
value_type* data() noexcept;const value_type* data() const noexcept;
Get pointer to data
Returns a pointer to the first element in the array object.
example
#include <iostream>#include <cstring>#include <array>int main (){const char* cstr = "Test string";std::array<char,12> charray;std::memcpy (charray.data(),cstr,12);std::cout << charray.data() << '\n';return 0;
reference at ( size_type n );const_reference at ( size_type n ) const;reference operator[] (size_type n);const_reference operator[] (size_type n) const;
reference front();const_reference front() const;reference back();const_reference back() const;
iterator begin() noexcept;const_iterator begin() const noexcept;iterator end() noexcept;const_iterator end() const noexcept;reverse_iterator rbegin() noexcept;const_reverse_iterator rbegin() const noexcept;reverse_iterator rend() noexcept;const_reverse_iterator rend() const noexcept;const_iterator cbegin() const noexcept;const_iterator cend() const noexcept;
constexpr bool empty() noexcept;void fill (const value_type& val); //sets val as the value for all the elements in the array object.
