std::vector<T,Allocator>::vector
| (1) | ||
|   vector();  | 
(C++17 前) | |
|   vector() noexcept(noexcept(Allocator()));  | 
 (C++17 起)  (C++20 前)  | 
|
|   constexpr vector() noexcept(noexcept(Allocator()));  | 
(C++20 起) | |
| (2) | ||
|   explicit vector( const Allocator& alloc );  | 
(C++17 前) | |
|   explicit vector( const Allocator& alloc ) noexcept;  | 
 (C++17 起)  (C++20 前)  | 
|
|   constexpr explicit vector( const Allocator& alloc ) noexcept;  | 
(C++20 起) | |
| (3) | ||
|   explicit vector( size_type count,                   const T& value = T(),  | 
(C++11 前) | |
|   vector( size_type count,                   const T& value,  | 
 (C++11 起)  (C++20 前)  | 
|
|   constexpr vector( size_type count,                    const T& value,  | 
(C++20 起) | |
| (4) | ||
|   explicit vector( size_type count );  | 
 (C++11 起)  (C++14 前)  | 
|
|   explicit vector( size_type count, const Allocator& alloc = Allocator() );  | 
 (C++14 起)  (C++20 前)  | 
|
|   constexpr explicit vector( size_type count, const Allocator& alloc = Allocator() );  | 
(C++20 起) | |
| (5) | ||
|   template< class InputIt > vector( InputIt first, InputIt last,   | 
(C++20 前) | |
|   template< class InputIt > constexpr vector( InputIt first, InputIt last,   | 
(C++20 起) | |
| (6) | ||
|   vector( const vector& other );  | 
(C++20 前) | |
|   constexpr vector( const vector& other );  | 
(C++20 起) | |
| (7) | ||
|   vector( const vector& other, const Allocator& alloc );  | 
 (C++11 起)  (C++20 前)  | 
|
|   constexpr vector( const vector& other, const Allocator& alloc );  | 
(C++20 起) | |
| (8) | ||
|   vector( vector&& other );  | 
 (C++11 起)  (C++17 前)  | 
|
|   vector( vector&& other ) noexcept;  | 
 (C++17 起)  (C++20 前)  | 
|
|   constexpr vector( vector&& other ) noexcept;  | 
(C++20 起) | |
| (9) | ||
|   vector( vector&& other, const Allocator& alloc );  | 
 (C++11 起)  (C++20 前)  | 
|
|   constexpr vector( vector&& other, const Allocator& alloc );  | 
(C++20 起) | |
| (10) | ||
|   vector( std::initializer_list<T> init,  const Allocator& alloc = Allocator() );  | 
 (C++11 起)  (C++20 前)  | 
|
|   constexpr vector( std::initializer_list<T> init,  const Allocator& alloc = Allocator() );  | 
(C++20 起) | |
从各种数据源构造新容器,可以使用用户提供的分配器 alloc。
[first, last) 内容的容器。
 | 
 如果   | 
(C++11 前) | 
| 
 此重载只有在  | 
(C++11 起) | 
| 
 如同通过调用  | 
(C++11 起) | 
| 
 在进行类模板实参推导时,只会从首个实参推导模板形参   | 
(C++23 起) | 
参数
| alloc | - | 用于此容器所有内存分配的分配器 | 
| count | - | 容器的大小 | 
| value | - | 以之初始化容器元素的值 | 
| first, last | - | 复制元素的来源范围 | 
| other | - | 用作初始化容器元素来源的另一容器 | 
| init | - | 用作初始化元素来源的初始化器列表 | 
| rg | - |  与容器兼容的范围,即元素可以转换到 T 的 input_range
 | 
复杂度
- 如果 first 和 last 都是前向,双向或随机访问迭代器,那么
 
-  只会调用 N  次 
T的复制构造函数,并且 - 不会进行重分配。
 
-  只会调用 N  次 
 
- 否则(first 和 last 都只是输入迭代器),
 
-  会调用 O(N) 次 
T的复制构造函数,并且 - 会进行 O(log N) 次重分配。
 
-  会调用 O(N) 次 
 
-  如果 
R实现了 ranges::forward_range 或 ranges::sized_range,那么 
- 从解引用 rg 的连续 N 个迭代器的结果初始化相同个数的元素,并且
 - 不会有重分配。
 
-  否则(
R实现了输入范围): 
-  调用 
T的复制或移动构造函数 O(N) 次,并且 - 会发生 O(log N) 次重分配。
 
-  调用 
 
异常
调用 Allocator::allocate 可能抛出。
注解
在容器移动构造(重载 (8))后,指向 other 的引用及迭代器(除了尾迭代器)保持合法,但指代现于 *this 中的元素。当前标准由 [container.requirements.general]/12 中的总括陈述作出此保证,而 LWG 问题 2321 正在考虑更严格的保证。
重载 (4) 对如 int 的非类类型元素清零,这与 new[] 将元素保持未初始化的行为不同。为匹配 new[] 的行为,可提供保留元素未初始化的自定义 Allocator::construct。
示例
#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 初始化器列表语法: 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 是 {"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]
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 | 
|---|---|---|---|
| LWG 134 | C++98 | 重载 (5) 在迭代器是输入迭代器的情况下最多只能调用 2N 次复制构造函数 | 改成调用 O(N) 次 | 
| LWG 868 | C++98 | 对于重载 (4),容器中的元素会被默认构造 | 它们会被值初始化 | 
| LWG 2193 | C++11 | 默认构造函数是 explicit 的 | 它是非 explicit 的 | 
参阅
|    将值赋给容器   (公开成员函数)  | |
|    赋值给容器   (公开成员函数)  |