STL Vector

Programming 2008/01/22 23:12
















Vector

Vectors are a kind of sequence containers. As such, their elements are ordered following a strict linear sequence.

Vector containers are implemented as dynamic arrays; Just as regular arrays, vector containers have their elements stored in contiguous storage locations, which means that their elements can be accessed not only using iterators but also using offsets on regular pointers to elements.

But unlike regular arrays, storage in vectors is handled automatically, allowing it to be expanded and contracted as needed.

Vectors are good at:

  • Accessing individual elements by their position index (constant time).
  • Iterating over the elements in any order (linear time).
  • Add and remove elements from its end (constant amortized time).

Compared to arrays, they provide almost the same performance for these tasks, plus they have the ability to be easily resized. Although, they usually consume more memory than arrays when their capacity is handled automatically (this is in order to accomodate for extra storage space for future growth).

Compared to the other base standard sequence containers (deques and lists), vectors are generally the most efficient in time for accessing elements and to add or remove elements from the end of the sequence. For operations that involve inserting or removing elements at positions other than the end, they perform worse than deques and lists, and have less consistent iterators and references than lists.

Internally, vectors -like all containers- have a size, which represents the amount of elements contained in the vector. But vectors, also have a capacity, which determines the amount of storage space they have allocated, and which can be either equal or greater than the actual size. The extra amount of storage allocated is not used, but is reserved for the vector to be used in the case it grows. This way, the vector does not have to reallocate storage on each occasion it grows, but only when this extra space is exhausted and a new element is inserted (which should only happen in logarithmic frequence in relation with its size).

Reallocations may be a costly operation in terms of performance, since they generally involve the entire storage space used by the vector to be copied to a new location. Therefore, whenever large increases in size are planned for a vector, it is recommended to explicitly indicate a capacity for the vector using member function vector::reserve.

In their implementation in the C++ Standard Template Library vectors take two template parameters:

template < class T, class Allocator = allocator<T> > class vector;
Where the template parameters have the following meanings:
  • T: Type of the elements.
  • Allocator: Type of the allocator object used to define the storage allocation model. By default, the allocator class template for type T is used, which defines the simplest memory allocation model and is value-independent.
In the reference for the vector member functions, these same names are assumed for the template parameters.

'Programming' 카테고리의 다른 글

Directshow strmbase.lib strmbasd.lib  (0) 2008/02/04
freetype dxfont 간 성능 비교..  (0) 2008/01/23
STL Vector  (0) 2008/01/22
hash string  (0) 2008/01/18
Texture Stage Argument  (0) 2008/01/18
FreeType2 library ft_pixel_mode_mono  (0) 2008/01/18
FreeType2 Library Metrics pixel 포맷  (0) 2008/01/18
freetype library  (0) 2007/12/14
rand(), srand(), RAND_MAX  (0) 2007/11/10
맵핑이란.. Mapping  (0) 2007/10/05
ID3DXSprite::Begin  (0) 2007/10/02
Posted by Hangenie

트랙백 주소 :: http://hangenie.com/trackback/277

댓글을 달아 주세요