博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++ vector
阅读量:5279 次
发布时间:2019-06-14

本文共 6026 字,大约阅读时间需要 20 分钟。

This article comes from there : 
 
Vector
Vectors are a kind of sequence container. 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 accommodate extra storage space for future growth).


Compared to the other base standard sequence containers (
s and 
s), 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 
s and 
s, and have less consistent iterators and references than 
s.


Internally, vectors -like most containers- have a 
, which represents the amount of elements contained in the vector. But vectors, also have a 
, which determines the amount of storage space they have allocated, and which can be either equal or greater than the actual 
. 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. You can use member function 
 to indicate beforehand a
 for the vector. This can help optimize storage space and reduce the number of reallocations when many enlargements are planned.


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

 
template < class T, class Allocator = allocator
> 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 allocatorclass 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.


Member functions

Construct vector 
(public member function)
Vector destructor 
(public member function)
Copy vector content 
(public member function )

Iterators
:

Return iterator to beginning 
(public member type)
Return iterator to end 
(public member function )
Return reverse iterator to reverse beginning 
(public member function)
Return reverse iterator to reverse end 
(public member function)

Capacity
:

Return size 
(public member function)
Return maximum size 
(public member function )
Change size 
(public member function)
Return size of allocated storage capacity 
(public member function)
Test whether vector is empty 
(public member function)
Request a change in capacity 
(public member function)

Element access
:

Access element 
(public member function)
Access element 
(public member function)
Access first element 
(public member function)
Access last element 
(public member function)

Modifiers
:

Assign vector content 
(public member function)
Add element at the end 
(public member function)
Delete last element 
(public member function)
Insert elements 
(public member function)
Erase elements 
(public member function )
Swap content 
(public member function )
Clear content 
(public member function)

Allocator
:

Get allocator 
(public member function )

Member types

of 
template <class T, class Allocator=allocator<T> > class vector;
 

member type definition
reference Allocator::reference
const_reference Allocator::const_reference
iterator Random access iterator
const_iterator Constant random access iterator
size_type Unsigned integral type (usually same as )
difference_type Signed integral type (usually same as )
value_type T
allocator_type Allocator
pointer Allocator::pointer
const_pointer Allocator::const_pointer
reverse_iterator reverse_iterator<iterator>
const_reverse_iterator reverse_iterator<const_iterator>

Vector specialization: vector<bool>

The vector class template has a special template specialization for the 
bool
 type.


This specialization is provided to optimize for space allocation: In this template specialization, each element occupies only one bit (which is eight times less than the smallest type in C++: 
char
).


The references to elements of a 
bool
 vector returned by the 
vector
 members are not references to 
bool
 objects, but a special member type which is a reference to a single bit, defined inside the 
vector<bool>
 class specialization as:


1 2 3 4 5 6 7 8 9 10
class vector
::reference { 
 friend class vector;
 reference();                                 // no public constructor 
public: 
 ~reference(); 
 operator bool () const;                      // convert to bool
 reference& operator= ( const bool x );       // assign from bool 
 reference& operator= ( const reference& x );  // assign from bit 
 void flip();                                 // flip bit value. 
}


For a similar container class to contain bits, but with a fixed size, see 
.

转载于:https://www.cnblogs.com/nickchan/archive/2012/03/30/3104432.html

你可能感兴趣的文章
移动、尺寸改变
查看>>
c# 文件笔记
查看>>
类和结构
查看>>
心得25--JDK新特性9-泛型1-加深介绍
查看>>
安装NVIDIA驱动时禁用自带nouveau驱动
查看>>
HDU-1255 覆盖的面积 (扫描线)
查看>>
Java 中 静态方法与非静态方法的区别
查看>>
Jenkins+ProGet+Windows Batch搭建全自动的内部包(NuGet)打包和推送及管理平台
查看>>
线程池的概念
查看>>
Java 序列化
查看>>
Java 时间处理实例
查看>>
Java 多线程编程
查看>>
Java 数组实例
查看>>
mysql启动过程
查看>>
2017前端面试题总结
查看>>
Http GetPost网络请求
查看>>
SWIFT国际资金清算系统
查看>>
Sping注解:注解和含义
查看>>
站立会议第四天
查看>>
如何快速掌握一门技术
查看>>