STL vector的构造函数和析构函数(2)
std::vector::vector
| 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, |
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的分配器
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
|
|
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.
迭代器的有效性:
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.
std::vector::~vector
~vector();
Destroys the container object.
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)的更多相关文章
- STL Vector使用
http://blog.163.com/zhoumhan_0351/blog/static/399542272010225104536463 Vector 像一个快速的数组,其具有数组的快速索引方式. ...
- STL——vector和list
vector和list为STL中的顺序容器,顺序容器会依次维护第一个到最后一个元素,在顺序容器上,我们主要的操作就是迭代. 头文件: #include<vector> #include&l ...
- C++ STL vector容器学习
STL(Standard Template Library)标准模板库是C++最重要的组成部分,它提供了一组表示容器.迭代器.函数对象和算法的模板.其中容器是存储类型相同的数据的结构(如vector, ...
- STL vector
STL vector vector是线性容器,它的元素严格的按照线性序列排序,和动态数组很相似,和数组一样,它的元素存储在一块连续的存储空间中,这也意味着我们不仅可以使用迭代器(iterator)访问 ...
- STL vector用法介绍
STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和f ...
- C++的优秀特性3:构造函数和析构函数
(转载请注明原创于潘多拉盒子) 构造函数和析构函数是C++中再熟悉不过的概念了,几乎每个了解一点C++的人都知道这两个概念是什么意思.一个对象的全部生命期中构造函数和析构函数执行的时机如下: 1. 为 ...
- C++的构造函数和析构函数
1.构造函数和析构函数为什么没有返回值? 构造函数和析构函数是两个非常特殊的函数:它们没有返回值.这与返回值为void的函数显然不同,后者虽然也不返回任何值,但还可以让它做点别的事情,而构造函数和析构 ...
- (转载)C++中将构造函数或析构函数定义为private
(转载)http://www.blogjava.net/fhtdy2004/archive/2009/05/30/278971.html C++中将构造函数或析构函数定义为private 很多情况下要 ...
- STL vector 用法介绍
介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...
随机推荐
- 7/25 CSU-ACM2018暑假集训比赛1
题目链接 [A - Tricky Sum ] In this problem you are to calculate the sum of all integers from 1 to n, but ...
- HDU 多校1.10
- 百度之星资格赛 2016 Problem 1001
本文链接http://www.cnblogs.com/Ash-ly/p/5494618.html 题意: 度熊手上有一本字典存储了大量的单词,有一次,他把所有单词组成了一个很长很长的字符串.现在麻烦来 ...
- 二维偏序+树状数组【P3431】[POI2005]AUT-The Bus
Description Byte City 的街道形成了一个标准的棋盘网络 – 他们要么是北南走向要么就是西东走向. 北南走向的路口从 1 到 n编号, 西东走向的路从1 到 m编号. 每个路口用两个 ...
- 有哪些适合新手练手的Python项目?
http://blog.csdn.net/Lina_ACM/article/details/54581721
- [BZOJ1095]捉迷藏
点了动态点分治的科技树,这道题是树形态不变的动态点分治,形态变化的话...待会补 考虑点分治过程中的这样一种结构:按递归层次把当前层的重心与上层重心互相连接,这就是点分治树,容易看出它的树高只有$O( ...
- 【第二类Stirling数】Gym - 101147G - The Galactic Olympics
如果K>n,就无解: 如果K==n,就答案是P(n,n): 如果K<n,答案就是s(n,K)*P(K,K): P为排列数,s为第二类斯特林数. 第二类斯特林数就是将n个球,划分为K个非空集 ...
- 【动态规划】Codeforces Round #392 (Div. 2) D. Ability To Convert
D. Ability To Convert time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 【二维树状数组】bzoj1452 [JSOI2009]Count
权值的种类只有100种,对每种开个二维BIT,然后是经典操作. #include<cstdio> using namespace std; ][]; struct BIT_2D { ][] ...
- python3中zipfile模块的常用方法
一.zipfile模块的简述 zipfile是python里用来做zip格式编码的压缩和解压缩的,由于是很常见的zip格式,所以这个模块使用频率也是比较高的, 在这里对zipfile的使用方法做一些记 ...