在线文档教程
C++
容器 | Containers

std::vector::vector

STD::向量:

(1)
explicit vector( const Allocator& alloc = Allocator() (until C++14)
vector() : vector( Allocator() ) {} explicit vector( const Allocator& alloc (since C++14) (until C++17)
vector() noexcept(noexcept(Allocator())): vector( Allocator() ) {} explicit vector( const Allocator& alloc ) noexcept;(since C++17)
(2)
explicit vector( size_type count, const T& value = T(), const Allocator& alloc = Allocator()(until C++11)
vector( size_type count, const T& value, const Allocator& alloc = Allocator()(since C++11)
(3)
explicit vector( size_type count (since C++11) (until C++14)
explicit vector( size_type count, const Allocator& alloc = Allocator() (since C++14)
template< class InputIt > vector( InputIt first, InputIt last, const Allocator& alloc = Allocator() (4)
vector( const vector& other (5)
vector( const vector& other, const Allocator& alloc (5)(since C++11)
(6)
vector( vector&& other (since C++11) (until C++17)
vector( vector&& other ) noexcept;(since C++17)
vector( vector&& other, const Allocator& alloc (7)(since C++11)
vector( std::initializer_list<T> init, const Allocator& alloc = Allocator() (8)(since C++11)

从各种数据源构造一个新容器,可以选择使用用户提供的分配器。alloc...

1%29默认构造函数。构造空容器。

2%29用count具有价值的元素的副本value...

3%29用count默认插入实例T.不制作副本。

4%29构造包含范围内容的容器。[first, last)...

This constructor has the same effect as vector(static_cast(first), static_cast(last), a) if InputIt is an integral type.(until C++11)
This overload only participates in overload resolution if InputIt satisfies InputIterator, to avoid ambiguity with the overload (2).(since C++11)

5%29复制构造函数。的内容的副本构造容器。other.如果alloc如果不提供分配器,则获得分配器时,就像通过调用std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator())...

6%29移动构造函数。的内容构造容器。other使用移动语义。分配器是通过移动构造从分配器获得的,该分配器属于other...

7%29分配器-扩展移动构造函数。使用alloc作为新容器的分配器,将内容从other;如果alloc != other.get_allocator(),这就导致了一个元素上的移动。

8%29使用初始化程序列表的内容构造容器。init...

参数

alloc-allocator to use for all memory allocations of this container
count-the size of the container
value-the value to initialize elements of the container with
first, last-the range to copy the elements from
other-another container to be used as source to initialize the elements of the container with
init-initializer list to initialize the elements of the container with

复杂性

1%29常数

2-3%29线性count

4%29直线距离firstlast

5%29线性other

6%29常数。

7%29线性IFalloc != other.get_allocator(),否则不变。

8%29线性init...

注记

在容器移动构造%28重载%286%29%29之后,引用、指针和迭代器%28---other保持有效,但引用当前在*this.现行标准通过第23.2.1节中的总括声明作出这一保证。集装箱。所需经费/12,目前正在考虑通过以下方式提供更直接的担保:lwg 2321...

重载%283%29从非类类型的元素(如int,这与new[],这使得它们没有初始化。与…的行为相匹配new[],一自定义分配器::构造可以提供这样的元素,而这些元素没有初始化。

二次

#include <vector> #include <string> #include <iostream> template<typename T> std::ostream& operator<<(std::ostream& s, const std::vector<T>& v) { s.put('[' char comma[3] = {'\0', ' ', '\0'}; for (const auto& e : v) { s << comma << e; comma[0] = ','; } return s << ']'; } int main() { // c++11 initializer list syntax: std::vector<std::string> words1 {"the", "frogurt", "is", "also", "cursed"}; std::cout << "words1: " << words1 << '\n'; // words2 == words1 std::vector<std::string> words2(words1.begin(), words1.end() std::cout << "words2: " << words2 << '\n'; // words3 == words1 std::vector<std::string> words3(words1 std::cout << "words3: " << words3 << '\n'; // words4 is {"Mo", "Mo", "Mo", "Mo", "Mo"} std::vector<std::string> words4(5, "Mo" std::cout << "words4: " << words4 << '\n'; }

二次

产出:

二次

words1: [the, frogurt, is, also, cursed] words2: [the, frogurt, is, also, cursed] words3: [the, frogurt, is, also, cursed] words4: [Mo, Mo, Mo, Mo, Mo]

二次

另见

assignassigns values to the container (public member function)
operator=assigns values to the container (public member function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/容器/载体/向量