ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

std::vector::vector//周末进行翻译//代码来自c plus plus

2021-05-14 14:33:01  阅读:320  来源: 互联网

标签:std elements container vector plus type allocator


public member function <vector>

std::vector::vector

default (1)
explicit vector (const allocator_type& alloc = allocator_type());
fill (2)
explicit vector (size_type n, const value_type& val = value_type(),
                 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);
Construct vector

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

(1) empty container constructor (default constructor)
Constructs an empty container, with no elements.
(2) fill constructor
Constructs a container with n elements. Each element is a copy of val.
(3) range constructor
Constructs a container with as many elements as the range [first,last), with each element constructed from its corresponding element in that range, in the same order.
(4) copy constructor
Constructs a container with a copy of each of the elements in x, in the same order.

The container keeps an internal copy of alloc, which is used to allocate storage throughout its lifetime.
The copy constructor (4) creates a container that keeps and uses a copy of x's allocator.

The storage for the elements is allocated using this internal allocator.

 

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).
If allocator_type is an instantiation of the default allocator (which has no state), this is not relevant.
n
Initial container size (i.e., the number of elements in the container at construction).
Member type size_type is an unsigned integral type.
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).
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.
x
Another vector object of the same type (with the same class template arguments T and Alloc), whose contents are either copied or acquired.
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
// constructing vectors
#include <iostream>
#include <vector>

int main ()
{
  // 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.

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.

See also

标签:std,elements,container,vector,plus,type,allocator
来源: https://www.cnblogs.com/fenyuxingzhe/p/14768431.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有