译文:
public member function
vector的构造器:这里我仅仅翻译C++11的,C++98的就不翻译了。
构造器原型:
<vector>
- C++98
- C++11
default (1) | explicit vector (const allocator_type& alloc = allocator_type()); |
fill (2) | explicit vector (size_type n); |
range (3) | template <class InputIterator> |
copy (4) | vector (const vector& x); |
move (5) | vector (vector&& x); |
initializer list (6) | vector (initializer_list<value_type> il, |
Construct vector
Constructs a vector, initializing its contents depending on the constructor version used:
- C++98
- C++11
(1) empty container constructor (default constructor) 原型:explicit vector (const allocator_type& alloc = allocator_type()); Constructs an empty container, with no elements 空參数构造器,默认的构造器 样例:vector<int> vi; (2) fill constructor 原型:
explicit vector (size_type n);
vector (size_type n, const value_type& val,
const allocator_type& alloc = allocator_type());
Constructs a container with n elements. Each element is a copy of val (if provided). 声明一个有n个元素的构造器,假设提供了初始值。则每个元素的值都是那个提供的值 样例: vector<int> vi(10);//声明一个拥有10个int元素的vector; vector<int> vi(10,55);//声明一个拥有10个int元素的vector,每个元素的值都是55;
(3) range constructor 原型:
template <class InputIterator>
vector (InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type());
Constructs a container with as many elements as the range [first,last), with each element emplace-constructed from its corresponding element in that range, in the same order. 构造一个容器能存放与[first,lase)区间的元素数目一样的vector,存放的元素的值以及位置和[first,lase)中全然一样。方向也要一致; 样例: vector<int> v1={10,20,30,40};//v1中存放的数组为{10,20,30,40}; vector<int> v2(v1.begin(),v1.end());//v2相当于从v1.begin()開始,一直相应复制v1里面的每个元素,直到迭代器为v1.end(); (4) copy constructor (and copying with allocator) 原型:
vector (const vector& x);
vector (const vector& x, const allocator_type& alloc);
Constructs a container with a copy of each of the elements in x, in the same order. 复制构造函数,逐个复制x中的每个元素。存放到本数组中。方向一致。 样例: vector<int> v1={10,20,30,40};//v1中存放的数组为{10,20,30,40}; vector<int> v2(v1);//复制v1中全部元素,存放到v2中, (5) move constructor (and moving with allocator) 原型:
vector (vector&& x);
vector (vector&& x, const allocator_type& alloc);
Constructs a container that acquires the elements of x.
If alloc is specified and is different from x's allocator, the elements are moved. Otherwise, no elements are constructed (their ownership is directly transferred).
x is left in an unspecified but valid state.//本条不是按原意翻译,仅仅是为了更好地说明 移动构造函数。
(使用C++11中的移动语义实现)//不知移动语义为何物的能够google一下
将右值引用中x的全部元素移动到新的vector中,以避免代价昂贵的复制。 //原意 假设新的分配器和x的分配器不一样,那么将移动x里面的元素到新的vector。假设分配器是一样的,那么将直接转换其全部权 样例:(样例有点难举。) vector<int> v2(參数是一个右值引用(比方一个暂时变量)) (6) initializer list constructor 原型:
vector (initializer_list<value_type> il,
const allocator_type& alloc = allocator_type());
Constructs a container with a copy of each of the elements in il, in the same order.初始化列表构造器,从初始化列表il中复制每个元素。放到vector中,方向一致。
样例: vector<int> v1({10,20,30,40});//v1的值为{10,20,30,40} The container keeps an internal copy of alloc, which is used to allocate and deallocate storage for its elements, and to construct and destroy them (as specified by its allocator_traits).
//这段可能不怎么正确。但意思是差点儿相同的
容器分配器使用元素内置的分配器创建容器里面的元素,析构时也是一样,比如假设里面元素是int。那么分配器也是使用int的分配器
The copy constructor (4, first signature) creates a container that keeps and uses a copy of the allocator returned by calling the appropriate selected_on_container_copy_construction trait on x's allocator.
复制构造函数(第4种)创建一个使用和x一样的分配器。
The move constructor (5, first signature) acquires x's allocator.All elements are copied, moved or otherwise constructed by calling allocator_traits::construct with the appropriate arguments.
移动构造函数(第5种)假设使用x的分配器。那么将是全部权的转移,假设是其它的分配器,那么将移动原来的元素。
Parameters
參数说明:
alloc Allocator object.
The container keeps and uses an internal copy of this allocator.
分配器对象
容器使用并保持一个内在的复制分配器。
Member type allocator_type is the internal allocator type used by the container, defined in vector as an alias of its second template parameter (Alloc).
allocator_type是一种容器使用的内在的分配器类型,作为vector模板參数里面的第二个參数。
If allocator_type is an instantiation of the default allocator (which has no state), this is not relevant.//这句不太会翻译
假设allocator_type是默认的实例化分配器(没有状态),这是不相关的。
n Initial container size (i.e., the number of elements in the container at construction).
Member type size_type is an unsigned integral type.
容器最初的大小,(即最開始容器存放的元素数目)
n是一个无符号整形类型。 val Value to fill the container with. Each of the n elements in the container will be initialized to a copy of this value.
Member type value_type is the type of the elements in the container, defined in vector as an alias of its first template parameter (T). 容器初始化时里面每个元素的初始化值。 val的值类型必须和模板參数里面的參数《T》一致 first, last
Input iterators to the initial and final positions in a range. The range used is [first,last), which includes all the elements between
first and last, including the element pointed by first but not the element pointed bylast.
The function template argument InputIterator shall be an input iterator type that points to elements of a type from which value_type objects can be constructed. 输入迭代器開始和结束的范围。这是一个开区间的范围[first,last),包含first--end里面的全部元素。包含first,可是不包含last x Another
vector object of the same type (with the same class template arguments T and Alloc), whose contents are either copied or acquired.
还有一个同类型(T和Alloc)vector数组的名称 il An initializer_list object.
These objects are automatically constructed from initializer list declarators.
Member type value_type is the type of the elements in the container, defined in vector as an alias of its first template parameter (T). 一个初始化列表对象。
Example
1 | // constructing vectors |
Output:
The contents of fifth are: 16 2 77 29 |
Complexity
Constant for the default constructor (1), and for the move constructors (5) (unless alloc is different from x's allocator).除了默认的构造器和移动构造器
For all other cases, linear in the resulting container size.其它的情况下,生成容器都在线性时间内。
Additionally, if InputIterator in the range constructor (3) is not at least of a forward iterator category (i.e., it is just an
input iterator), the new capacity cannot be determined beforehand and the construction incurs in additional logarithmic complexity in
size (reallocations while growing).
//翻译可能不太准确
此外,假设输入构造器至少不属于前向迭代器。那么新的容量不能事先确定而且当增长时重分配容量将在对数时间内完毕。
Iterator validity
The move constructors (5), invalidate all iterators, pointers and references related to x if the elements are moved.
迭代器的有效性:
除了移动构造函数,不论什么指向以及引用的x的迭代器,在元素移动时都将失效。
Data races
All copied elements are accessed.
The move constructors (5) modify x.
数据争用:
全部元素复制訪问。
移动复制构造函数除外。
Exception safety
Strong guarantee: no effects in case an exception is thrown.
If allocator_traits::construct is not supported with the appropriate arguments for the element constructions, or if the range specified by [first,last) is not valid, it causes
undefined behavior.
异常安全性:
强有力的保证。没有异常被抛出。
假设分配器不支持元素的默认的构造函数(合适的參数构造函数),或者是超出了范围的迭代器。都将导致不确定的行为。
public member function
<vector>
析构函数
~vector();
Vector destructor
Destroys the container object.
- C++98
- C++11
This calls allocator_traits::destroy on each of the contained elements, and deallocates all the storage capacityallocated by the vector using its allocator.
使用元素自带的析构函数析构里面的每个元素。
Complexity
Linear in vector::size (destructors).
Iterator validity
All iterators, pointers and references are invalidated.
Data races
The container and all its elements are modified.
Exception safety
No-throw guarantee: never throws exceptions.
//翻译不一定非常正确,仅仅能保证大意正确,请多体谅。
.