[关闭]
@zhengyuhong 2015-06-09T09:58:46.000000Z 字数 1414 阅读 1075

array

C++11 STL


array

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

  std::array是一个支持随机访问且大小(size)固定的容器(译注:可以认为是一个紧缩版的vector吧)。它有如下特点:

  1. array<int,6> a = { 1, 2, 3 };
  2. a[3]=4;
  3. int x = a[5]; // array的默认数据元素为0,所以x的值变成0
  4. int* p1 = a; // 错误: std::array不能隐式地转换为指针
  5. int* p2 = a.data(); // 正确,data()得到指向第一个元素的指针

array::data

  1. value_type* data() noexcept;
  2. const value_type* data() const noexcept;

Get pointer to data
Returns a pointer to the first element in the array object.

example

  1. #include <iostream>
  2. #include <cstring>
  3. #include <array>
  4. int main ()
  5. {
  6. const char* cstr = "Test string";
  7. std::array<char,12> charray;
  8. std::memcpy (charray.data(),cstr,12);
  9. std::cout << charray.data() << '\n';
  10. return 0;

array::public member function

  1. reference at ( size_type n );
  2. const_reference at ( size_type n ) const;
  3. reference operator[] (size_type n);
  4. const_reference operator[] (size_type n) const;
  1. reference front();
  2. const_reference front() const;
  3. reference back();
  4. const_reference back() const;
  1. iterator begin() noexcept;
  2. const_iterator begin() const noexcept;
  3. iterator end() noexcept;
  4. const_iterator end() const noexcept;
  5. reverse_iterator rbegin() noexcept;
  6. const_reverse_iterator rbegin() const noexcept;
  7. reverse_iterator rend() noexcept;
  8. const_reverse_iterator rend() const noexcept;
  9. const_iterator cbegin() const noexcept;
  10. const_iterator cend() const noexcept;
  1. constexpr bool empty() noexcept;
  2. void fill (const value_type& val); //sets val as the value for all the elements in the array object.
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注