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()中的使用.通 ...
随机推荐
- (20)python pycharm
使用GitHub 一·登录GitHub 1. 2. 3. 4. 二. 登录成功后再配置git 1 2.创建项目到github 3.下载github
- Error converting bytecode to dex: Cause: java.lang.RuntimeException: Exception parsing classes
http://blog.csdn.net/xx326664162/article/details/51859106 总算有个靠谱的了
- 【转】vector用法
原博地址:http://www.cnblogs.com/wang7/archive/2012/04/27/2474138.html 在c++中,vector是一个十分有用的容器,下面对这个容器做一下总 ...
- English distilled
[ Any question about the job]关于辞职原因 1. What are the main objectives and responsibilities of the posi ...
- [CTSC2017]吉夫特(Lucas定理,DP)
送70分,预处理组合数是否为偶数即可. 剩下的数据,根据Lucas定理的推论可得当且仅当n&m=n的时候,C(n,m)为奇数.这样就可以直接DP了,对于每个数,考虑它对后面的数的影响即可,直接 ...
- Spring Boot中使用MyBatis注解配置详解
传参方式 下面通过几种不同传参方式来实现前文中实现的插入操作. 使用@Param 在之前的整合示例中我们已经使用了这种最简单的传参方式,如下: @Insert("INSERT INTO US ...
- 使用jmatio读写matlab数据文件
/** * Created by hfz on 2016/2/23. */ import com.jmatio.io.*; import com.jmatio.types.MLDouble; impo ...
- log4j.properties配置详解与实例-全部测试通过
最近使用log4j写log时候发现网上的写的都是千篇一律,写的好的嘛不全,写的全一点的嘛没有一点格式,看着累.这里把网上收集到的整理了一下,并且全部都在机器上测试成功了.这么好的文档估计没有了吧? # ...
- css自动换行与不换行
1.自动换行 div{ word-wrap: break-word; word-break: normal; } 2.不换行 div{ white-space:nowrap; } 3.浮动效果不换行 ...
- iOS:Objective-c的MD5/SHA1加密算法的实现
介绍: Objective-c实现MD5和SHA1算法相对还是比较简单的,可以直接调用系统的C/C++共享库来实现调用MD5即Message Digest Algorithm 5(信息-摘要算法 5) ...