C++ 参考手册

位置:首页 > C++ 参考手册 >容器库 >std::vector > std::vector<T,Allocator>::vector

(1)
vector();
(C++17 前)
vector() noexcept(noexcept(Allocator()));
(C++17 起)
(2)
explicit vector( const Allocator& alloc );
(C++17 前)
explicit vector( const Allocator& alloc ) noexcept;
(C++17 起)
(3)
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 起)
(4)
explicit vector( size_type count );
(C++11 起)
(C++14 前)
explicit vector( size_type count, const Allocator& alloc = Allocator() );
(C++14 起)
template< class InputIt >

vector( InputIt first, InputIt last,

        const Allocator& alloc = Allocator() );
(5)
vector( const vector& other );
(6)
vector( const vector& other, const Allocator& alloc );
(6) (C++11 起)
(7)
vector( vector&& other );
(C++11 起)
(C++17 前)
vector( vector&& other ) noexcept;
(C++17 起)
vector( vector&& other, const Allocator& alloc );
(8) (C++11 起)
vector( std::initializer_list<T> init,
        const Allocator& alloc = Allocator() );
(9) (C++11 起)

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

1) 默认构造函数。构造拥有默认构造的分配器的空容器。
2) 构造拥有给定分配器 alloc 的空容器。
3) 构造拥有 count 个有值 value 的元素的容器。
4) 构造拥有个 count 默认插入T 实例的容器。不进行复制。
5) 构造拥有范围 [first, last) 内容的容器。

InputIt 是整数类型,则此构造函数拥有的效果同 vector(static_cast<size_type>(first), static_cast<value_type>(last), a)

(C++11 前)

此重载仅若InputIt 满足遗留输入迭代器 (LegacyInputIterator) 才参与重载决议,以避免和重载 (2) 的歧义。

(C++11 起)
6) 复制构造函数。构造拥有 other 内容的容器。若不提供 alloc ,则如同通过调用 std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()) 获得分配器。
7) 移动构造函数。用移动语义构造拥有 other 内容的容器。分配器通过属于 other 的分配器移动构造获得。移动后,保证 otherempty()
8) 有分配器扩展的移动构造函数。以 alloc 为新容器的分配器,从 other 移动内容;若 alloc != other.get_allocator() ,则它导致逐元素移动。(该情况下,移动后不保证 other 为空)
9) 构造拥有 initializer_list init 内容的容器。

参数

alloc - 用于此容器所有内存分配的分配器
count - 容器的大小
value - 以之初始化容器元素的值
first, last - 复制元素的来源范围
other - 用作初始化容器元素来源的另一容器
init - 用作初始化元素来源的 initializer_list

复杂度

1-2) 常数
3-4)count 成线性
5)firstlast 的距离成线性
6)other 的大小成线性
7) 常数。
8)alloc != other.get_allocator() 则为线性,否则为常数。
9)init 的大小成线性。

异常

Allocator::allocate 的调用可能抛出。

注意

在容器移动构造(重载 (7) )后,指向 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++ 标准。

DR 应用于 出版时的行为 正确行为
LWG 2193 C++11 默认构造函数为 explicit 使之为非 explicit

参阅

将值赋给容器
(公开成员函数)
赋值给容器
(公开成员函数)