代码改变世界

STL array

2012-07-15 14:35  youxin  阅读(343)  评论(0编辑  收藏  举报

 先声明,这个STL 容器很少有人使用,因为必须要指定大小,一般都用vector或list 了。

template < class T, size_t N > class array;

Container properties

Sequence
Elements in sequence containers are ordered in a strict linear sequence. Individual elements are accessed by their position in this sequence.
Contiguous storage(连续的)
The elements are stored in contiguous memory locations, allowing constant time random access to elements. Pointers to an element can be offset to access other elements.
Fixed-size aggregate(集合,聚集)
The container uses implicit constructors and destructors to allocate the required space statically. Its size is compile-time constant. No memory or time overhead.
用法和vector差不多:
// array::begin example
#include <iostream>
#include <array>

int main ()
{
  std::array<int,5> myarray = { 2, 16, 77, 34, 50 };

  std::cout << "myarray contains:";
  for ( auto it = myarray.begin(); it != myarray.end(); ++it )
    std::cout << " " << *it;

  std::cout << std::endl;

  return 0;
}