STL vector的构造函数和析构函数(2)

原文来自: 点击打开链接
译文:
public member function
vector的构造器:这里我只翻译C++11的,C++98的就不翻译了。
构造器原型:

std::vector::vector

  • C++98
  • C++11
default (1)
explicit vector (const allocator_type& alloc = allocator_type());
fill (2)
explicit vector (size_type n);
         vector (size_type n, const value_type& val,
                 const allocator_type& alloc = allocator_type());
range (3)
template 
  vector (InputIterator first, InputIterator last,
          const allocator_type& alloc = allocator_type());
copy (4)
vector (const vector& x);
vector (const vector& x, const allocator_type& alloc);
move (5)
vector (vector&& x);
vector (vector&& x, const allocator_type& alloc);
initializer list (6)
vector (initializer_list il,
       const allocator_type& alloc = allocator_type());
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 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 vi(10);//声明一个拥有10个int元素的vector; vector vi(10,55);//声明一个拥有10个int元素的vector,每一个元素的值都是55;
(3) range constructor 原型:
template 
  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 v1={10,20,30,40};//v1中存放的数组为{10,20,30,40}; vector 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 v1={10,20,30,40};//v1中存放的数组为{10,20,30,40}; vector 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 v2(参数是一个右值引用(比如一个临时变量))(6) initializer list constructor原型:
vector (initializer_list 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 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  copiedmoved 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 by last.
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// constructing vectors
#include 
#include 

int main ()
{
  unsigned int i;

  // constructors used in the same order as described above:
  std::vector first;                                // empty vector of ints
  std::vector second (4,100);                       // four ints with value 100
  std::vector third (second.begin(),second.end());  // iterating through second
  std::vector fourth (third);                       // a copy of third

  // the iterator constructor can also be used to construct from arrays:
  int myints[] = {16,2,77,29};
  std::vector fifth (myints, myints + sizeof(myints) / sizeof(int) );

  std::cout << "The contents of fifth are:";
  for (std::vector::iterator it = fifth.begin(); it != fifth.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}
Edit & Run


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

std::vector::~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.


//翻译不一定很正确,只能保证大意正确,请多体谅。





.

你可能感兴趣的:(STL,containers,STL,容器接口系列译文)