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

原文来自:点击打开链接

译文:

public member function

vector的构造器:这里我只翻译C++11的,C++98的就不翻译了。

构造器原型:

<vector>

std::vector::vector

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 <class InputIterator>
  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<value_type> il,
       const allocator_type& alloc = allocator_type());

Construct vector

Constructs a vector, initializing its contents depending on the constructor version used:

(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 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 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
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 <iostream>
#include <vector>

int main ()
{
  unsigned int i;

  // constructors used in the same order as described above:
  std::vector<int> first;                                // empty vector of ints
  std::vector<int> second (4,100);                       // four ints with value 100
  std::vector<int> third (second.begin(),second.end());  // iterating through second
  std::vector<int> 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<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );

  std::cout << "The contents of fifth are:";
  for (std::vector<int>::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

<vector>

std::vector::~vector

析构函数

~vector();

Vector destructor

Destroys the container object.

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 vector的构造函数和析构函数(2),布布扣,bubuko.com

时间: 2024-08-06 15:58:40

STL vector的构造函数和析构函数(2)的相关文章

STL vector方法总结(一)Member functions(34)

这里是vector的所有构造方法,成员方法的一些总结,具体的可以详看后面的链接. 容器:Vector 原型: template < class T, class Alloc = allocator<T> > class vector; 描述:vector是一种顺序容器,其行为类似于大小可以改变的array数组. 跟array一样,vector使用连续的存储单元来存储里面的元素.这意味着vector可以使用正常的指针的偏移量来访问其元素.它跟array一样的高效,但是不同于array

STL ——vector 学习

STL简介 C++ STL (Standard Template Library标准模板库) 是通用类模板和算法的集合,它提供给程序员一些标准的数据结构的实现如 queues(队列), lists(链表), 和 stacks(栈)等.  C++ STL 提供给程序员以下三类数据结构的实现: 标准容器类   顺序性容器  vector 从后面快速的插入与删除,直接访问任何元素  deque 从前面或后面快速的插入与删除,直接访问任何元素 list 双链表,从任何地方快速插入与删除   关联容器  

STL vector,deque,list

一.vector可变长的动态数组必须包含头文件 #include <vector>支持随机访问迭代器• 根据下标随机访问某个元素时间为常数• 在尾部添加速度很快• 在中间插入慢所有STL算法 都能对vector操作 构造函数初始化:vector();无参构造函数, 将容器初始化成空的vector(int n);将容器初始化成有n个元素vector(int n, const T & val);假定元素类型是T, 将容器初始化成有n个元素, 每个元素的值都是valvector(iterat

C++ STL vector容器学习

STL(Standard Template Library)标准模板库是C++最重要的组成部分,它提供了一组表示容器.迭代器.函数对象和算法的模板.其中容器是存储类型相同的数据的结构(如vector,list, deque, set, map等),算法完成特定任务,迭代器用来遍历容器对象,扮演容器和算法之间的胶合剂. 模板类vector 在计算中,矢量(vector)对应数组,它的数据安排以及操作方式,与array非常类似.在C++中,使用vector模板类时,需要头文件包含#include<v

STL vector用法介绍

介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通过阅读这篇文章读者应该能够有效地使用vector容器,而且应该不会再去使用C类型的动态数组了.   Vector总览 vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象

C++ stl vector介绍

转自: STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通过阅读这篇文章读者应该能够有效地使用vector容器,而且应该不会再去使用C类型的动态数组了.   Vector总览 vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是

STL vector

author:Donald-hu    theme:STL vector 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通过阅读这篇文章读者应该能够有效地使用vector容器,而且应该不会再去使用C类型的动态数组了.   Vector总览 vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.v

STL vector使用方法介绍

介绍 这篇文章的目的是为了介绍std::vector,怎样恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通过阅读这篇文章读者应该可以有效地使用vector容器,并且应该不会再去使用C类型的动态数组了.   Vector总览 vector是C++标准模板库中的部分内容,它是一个多功能的,可以操作多种数据结构和算法的模板类和函数库.vector之所以被觉得是一个容器,是由于它可以像容器一样存放各种类型的对象

STL vector用法介绍(转)

介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通过阅读这篇文章读者应该能够有效地使用vector容器,而且应该不会再去使用C类型的动态数组了.   Vector总览 vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象