命名空间
变体
操作

std::vector<T,Allocator>::vector

来自 cppreference.cn
< cpp‎ | container‎ | vector
 
 
 
 
(1)
vector() : vector(Allocator()) {}
(since C++11)
(until C++17)
vector() noexcept(noexcept(Allocator())) : vector(Allocator()) {}
(since C++17)
(constexpr since C++20)
(2)
explicit vector( const Allocator& alloc = Allocator() );
(until C++11)
explicit vector( const Allocator& alloc );
(since C++11)
(noexcept since C++17)
(constexpr since C++20)
explicit vector( size_type count,
                 const Allocator& alloc = Allocator() );
(3) (since C++11)
(4)
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)
(constexpr since C++20)
template< class InputIt >

vector( InputIt first, InputIt last,

        const Allocator& alloc = Allocator() );
(5) (constexpr since C++20)
template< container-compatible-range<T> R >

constexpr vector( std::from_range_t, R&& rg,

                  const Allocator& alloc = Allocator() );
(6) (since C++23)
vector( const vector& other );
(7) (constexpr since C++20)
vector( vector&& other );
(8) (since C++11)
(noexcept since C++17)
(constexpr since C++20)
(9)
vector( const vector& other, const Allocator& alloc );
(since C++11)
(constexpr since C++20)
(until C++23)
constexpr vector( const vector& other,
                  const std::type_identity_t<Allocator>& alloc );
(since C++23)
(10)
vector( vector&& other, const Allocator& alloc );
(since C++11)
(until C++23)
constexpr vector( vector&& other,
                  const std::type_identity_t<Allocator>& alloc );
(since C++23)
vector( std::initializer_list<T> init,
        const Allocator& alloc = Allocator() );
(11) (since C++11)

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

1) C++11 以来的默认构造函数。构造一个带有默认构造分配器的空 vector
如果 Allocator 不是 DefaultConstructible,则行为未定义。
2) C++11 之前的默认构造函数。构造一个带有给定分配器 alloc 的空 vector
3) 构造一个 vector,其中包含 countT 的默认插入对象。不进行复制。
如果 T 不能 DefaultInsertablestd::vector<T> 中,则行为未定义。
4) 构造一个 vector,其中包含 count 个值为 value 的元素的副本。

如果 T 不能 CopyInsertablestd::vector<T> 中,则行为未定义。

(since C++11)
5) 构造一个 vector,其内容为范围 [firstlast) 的内容。范围 [firstlast) 中的每个迭代器都只被解引用一次。

如果 InputIt 不满足 LegacyInputIterator 的要求,则使用参数 static_cast<size_type>(first)lastalloc 调用重载 (4)

(until C++11)

仅当 InputIt 满足 LegacyInputIterator 的要求时,此重载才参与重载解析。

如果满足以下任何条件,则行为未定义

(since C++11)
6) 构造一个 vector,其内容为范围 rg 的内容。范围 rg 中的每个迭代器都只被解引用一次。
如果满足以下任何条件,则行为未定义
7-10) 构造一个 vector,其内容为 other 的内容。
7) 复制构造函数。

分配器通过调用 std::allocator_traits<Allocator>::
    select_on_container_copy_construction
        (other.get_allocator())
获得,如同调用此函数一样。

(since C++11)
8) 移动构造函数。分配器通过从 other.get_allocator() 移动构造获得。
9) 与复制构造函数相同,不同之处在于 alloc 用作分配器。
如果 T 不能 CopyInsertablestd::vector<T> 中,则行为未定义。
10) 与移动构造函数相同,不同之处在于 alloc 用作分配器。
如果 T 不能 MoveInsertablestd::vector<T> 中,则行为未定义。
11) 等效于 vector(il.begin(), il.end(), alloc)

内容

[编辑] 参数

alloc - 用于此容器的所有内存分配的分配器
count - 容器的大小
value - 用于初始化容器元素的value
first, last - 定义要从中复制元素的范围的迭代器对
other - 另一个容器,用作初始化容器元素的源
init - 用于初始化容器元素的初始化器列表
rg - 容器兼容范围

[编辑] 复杂度

1,2) 常数。
3,4)count 中呈线性。
5) 给定 std::distance(first, last)N
  • 如果 firstlast 都是前向、双向或随机访问迭代器,
  • T 的复制构造函数仅被调用 N  次,并且
  • 不会发生重新分配。
  • 否则(firstlast 只是输入迭代器),
  • T 的复制构造函数被调用 O(N) 次,并且
  • 重新分配发生 O(log N) 次。
6) 给定 ranges::distance(rg)N
  • 如果 R 建模 ranges::forward_rangeranges::sized_range
  • 从解引用 rg 的连续迭代器的结果初始化正好 N 个元素,并且
  • 不会发生重新分配。
  • 否则(R 建模输入范围),
  • T 的复制或移动构造函数被调用 O(N) 次,并且
  • 重新分配发生 O(log N) 次。
7)other.size() 中呈线性。
8) 常数。
9)other.size() 中呈线性。
10) 如果 alloc != other.get_allocator(),则在 other.size() 中呈线性,否则为常数。
11)init.size() 中呈线性。

[编辑] 异常

Allocator::allocate 的调用可能会抛出异常。

[编辑] 注解

在容器移动构造之后(重载 (8)),对 other 的引用、指针和迭代器(末尾迭代器除外)仍然有效,但引用现在位于 *this 中的元素。当前标准通过 [container.reqmts]/67 中的一般性声明以及通过 LWG issue 2321 正在考虑的更直接的保证来做出此保证。

特性测试 Std 特性
__cpp_lib_containers_ranges 202202L (C++23) 范围感知 构造和插入;重载 (6)

[编辑] 示例

#include <iostream>
#include <string>
#include <vector>
 
template<typename T>
std::ostream& operator<<(std::ostream& s, const std::vector<T>& v)
{
    s.put('{');
    for (char comma[]{'\0', ' ', '\0'}; const auto& e : v)
        s << comma << e, comma[0] = ',';
    return s << "}\n";
}
 
int main()
{
    // C++11 initializer list syntax:
    std::vector<std::string> words1{"the", "frogurt", "is", "also", "cursed"};
    std::cout << "1: " << words1;
 
    // words2 == words1
    std::vector<std::string> words2(words1.begin(), words1.end());
    std::cout << "2: " << words2;
 
    // words3 == words1
    std::vector<std::string> words3(words1);
    std::cout << "3: " << words3;
 
    // words4 is {"Mo", "Mo", "Mo", "Mo", "Mo"}
    std::vector<std::string> words4(5, "Mo");
    std::cout << "4: " << words4;
 
    const auto rg = {"cat", "cow", "crow"};
#ifdef __cpp_lib_containers_ranges
    std::vector<std::string> words5(std::from_range, rg); // overload (6)
#else
    std::vector<std::string> words5(rg.begin(), rg.end()); // overload (5)
#endif
    std::cout << "5: " << words5;
}

输出

1: {the, frogurt, is, also, cursed}
2: {the, frogurt, is, also, cursed}
3: {the, frogurt, is, also, cursed}
4: {Mo, Mo, Mo, Mo, Mo}
5: {cat, cow, crow}

[编辑] 缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

DR 应用于 已发布行为 正确行为
LWG 134 C++98 重载 (5) 允许在输入迭代器情况下最多 2N  次复制
构造函数调用
更改为 O(N) 次调用
LWG 438 C++98 重载 (5) 将仅调用重载 (4)
如果 InputIt 是整数类型
如果 InputIt
不是 LegacyInputIterator,则调用重载 (4)
LWG 2193 C++11 默认构造函数是显式的 变为非显式
LWG 2210 C++11 重载 (3) 没有分配器参数 添加了参数
N3346 C++11 对于重载 (3),容器中的元素是
容器中的元素是值初始化的
它们是默认插入的

[编辑] 参见

将值赋给容器
(public member function) [编辑]
将值赋给容器
(public member function) [编辑]