命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | 容器‎ | vector
 
 
 
 
(1)
vector() : vector(Allocator()) {}
(C++11 起)
(C++17 前)
vector() noexcept(noexcept(Allocator())) : vector(Allocator()) {}
(C++17 起)
(C++20 起为 constexpr)
(2)
explicit vector( const Allocator& alloc = Allocator() );
(C++11 前)
explicit vector( const Allocator& alloc );
(C++11 起)
(noexcept since C++17)
(C++20 起为 constexpr)
explicit vector( size_type count,
                 const Allocator& alloc = Allocator() );
(3) (C++11 起)
(4)
explicit vector( size_type count, const T& value = T(),
                 const Allocator& alloc = Allocator() );
(C++11 前)
vector( size_type count, const T& value,
        const Allocator& alloc = Allocator() );
(C++11 起)
(C++20 起为 constexpr)
template< class InputIt >

vector( InputIt first, InputIt last,

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

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

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

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

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

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

(C++11 起)
5) 构造一个包含范围 [firstlast) 内容的 vector[firstlast) 中的每个迭代器都只解引用一次。

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

(C++11 前)

此重载仅在 InputIt 满足 LegacyInputIterator 的要求时参与重载决议。

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

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

分配器是通过调用 std::allocator_traits<Allocator>::
    select_on_container_copy_construction
        (other.get_allocator())
获得的。

(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 - 用于初始化容器元素的数值
first, last - 定义要从中复制元素的 范围 的一对迭代器。
其他 - 另一个容器,用作初始化容器元素的源
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 考虑更直接的保证。

特性测试 标准 特性
__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++ 标准。

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

[编辑] 另请参阅

将值赋给容器
(public member function) [编辑]
将值赋给容器
(public member function) [编辑]
English Deutsch 日本語 한국어 中文(简体) 中文(繁體)