std::vector<T, Allocator>::vector
来自 zh.cppreference.net
vector() : vector(Allocator()) {}
|
(1) | (自 C++11) (直到 C++17) |
vector() noexcept(noexcept(Allocator())) : vector(Allocator()) {}
|
(自 C++17) (自 C++20 起为 constexpr) |
|
explicit vector( const Allocator& alloc = Allocator() );
|
(2) | (直到 C++11) |
explicit vector( const Allocator& alloc );
|
(自 C++11) (自 C++17 起为 noexcept) (自 C++20 起为 constexpr) |
|
explicit vector( size_type count,
const Allocator& alloc = Allocator() );
|
(3) | (自 C++11) |
explicit vector( size_type count, const T& value = T(),
const Allocator& alloc = Allocator() );
|
(4) | (直到 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) (自 C++17 起为 noexcept) (自 C++20 起为 constexpr) |
vector( const vector& other, const Allocator& alloc );
|
(9) | (自 C++11) (自 C++20 起为 constexpr) (直到 C++23) |
constexpr vector( const vector& other,
const std::type_identity_t<Allocator>& alloc );
|
(自 C++23) | |
vector( vector&& other, const Allocator& alloc );
|
(10) | (自 C++11) (自 C++20 起为 constexpr) (直到 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) 构造一个
vector,包含 count 个 T 的默认插入对象。不进行任何复制。4) 构造一个
vector,包含 count 个值为 value 的元素的副本。
|
如果 |
(自 C++11) |
5) 构造一个
vector,其内容为范围 [first, last) 中的元素。[first, last) 中的每个迭代器恰好解引用一次。
|
如果 |
(直到 C++11) |
|
仅当 如果满足以下任何条件,则行为未定义:
|
(自 C++11) |
6) 构造一个
vector,其内容为范围 rg 中的元素。rg 中的每个迭代器恰好解引用一次。 如果满足以下任何条件,则行为未定义:
T不是 EmplaceConstructible 到std::vector<T>中来自*ranges::begin(rg)的类型。R既不符合 sized_range 也不符合 forward_range,且T不是 MoveInsertable 到std::vector<T>中。
7-10) 构造一个
vector,其内容为 other 中的元素。7) 复制构造函数。
|
分配器的获取方式如同调用 |
(自 C++11) |
8) 移动构造函数。分配器通过从
other.get_allocator() 进行移动构造来获取。9) 与复制构造函数相同,但使用
alloc 作为分配器。10) 与移动构造函数相同,但使用
alloc 作为分配器。11) 等价于
vector(il.begin(), il.end(), alloc)。参数
| alloc | - | 用于此容器所有内存分配的分配器 |
| count | - | 容器的大小 |
| value | - | 用于初始化容器元素的值 |
| first, last | - | 定义要复制元素范围的迭代器对 |
| other | - | 用作源容器以初始化当前容器元素的其他容器 |
| init | - | 用于初始化容器元素的初始化列表 |
| rg | - | 容器兼容范围 |
复杂度
1,2)
常量。
3,4)
与
count
呈线性关系。
5)
给定
std::
distance
(
first, last
)
为
N
:
-
若
InputIt满足 LegacyForwardIterator 的要求,则:
-
-
T的拷贝构造函数仅被调用 N 次 - 不会发生重新分配
-
- 否则:
-
-
T的拷贝构造函数被调用 O(N) 次 - 发生 O(log N) 次重新分配
-
6)
给定
ranges::
distance
(
rg
)
为
N
:
|
(C++26 前) |
|
(C++26 起) |
-
- 从 rg 的连续迭代器解引用结果中精确初始化 N 个元素。
- 不会发生重新分配。
- 否则:
-
-
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 初始化列表语法: 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 为 {"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); // 重载 (6) #else std::vector<std::string> words5(rg.begin(), rg.end()); // 重载 (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) 次调用 |
| LWG 438 | C++98 |
重载
(
5
)
仅在
InputIt
为整型时才调用
重载 ( 4 ) |
当
InputIt
不是
LegacyInputIterator
时调用重载 ( 4 ) |
| LWG 2193 | C++11 | 默认构造函数为 explicit | 改为 non-explicit |
| LWG 2210 | C++11 | 重载 ( 3 ) 没有分配器参数 | 添加了该参数 |
| N3346 | C++11 |
对于重载
(
3
)
,容器中的元素
采用值初始化 |
改为默认插入 |
参见
|
为容器赋值
(公开成员函数) |
|
|
为容器赋值
(公开成员函数) |