STL-vector-vector构造函数

功能:构造 vector

vector();

vector() noexcept(noexcept(Allocator()));

(1)
(直到 C++17)

(自 C++17)
(自 C++20 起为 constexpr)
explicit vector( const Allocator& alloc );

explicit vector( const Allocator& alloc ) noexcept;
(2)(直到 C++17)

(自 C++17)
(自 C++20 起为 constexpr)
explicit vector( size_type count, const T& value = T(),
const Allocator& alloc = Allocator() );

vector( size_type count,
        const T& value,
        const Allocator& alloc = Allocator() );
(3)(直到 C++11)

(自 C++11)
(自 C++20 起为 constexpr)
explicit vector( size_type count );

explicit vector( size_type count,
                 const Allocator& alloc = Allocator() );
(4)(自 C++11)
(直到 C++14)

(自 C++11)
(直到 C++14)
template< class InputIt >
vector( InputIt first, InputIt last,
        const Allocator& alloc = Allocator() );
(5)(自 C++20 起为 constexpr)
vector( const vector& other );(6)(自 C++20 起为 constexpr)
vector(const vector& other,const Allocator& alloc );(7)(自 C++11)
(自 C++20 起为 constexpr)
vector( vector&& other );(8)(自 C++11)
(自 C++17 起为 noexcept)
(自 C++20 起为 constexpr)
vector( vector&& other, const Allocator& alloc );(9)(自 C++11)
(自 C++20 起为 constexpr)
vector( std::initializer_list<T> init,
        const Allocator& alloc = Allocator() );
(10)(自 C++11)
(自 C++20 起为 constexpr)
template< container-compatible-range<T> R >
constexpr vector( std::from_range_t, R&& rg,
                  const Allocator& alloc = Allocator() );
(11)(自 C++23)

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

1) 默认构造函数。使用默认构造的分配器构造一个空容器。

2) 使用给定的分配器 alloc 构造一个空容器。

3) 使用值为 value 的 count 个元素的副本构造容器。

4) 使用 count 个 T 的 默认插入 实例构造容器。没有进行复制。

5) 使用范围 [firstlast) 的内容构造容器。

如果 InputIt 是一个整数类型,则此构造函数的效果与 vector(static_cast<size_type>(first), static_cast<value_type>(last), a) 相同。(直到 C++11)
此重载仅在 InputIt 满足 LegacyInputIterator 时才参与重载解析,以避免与重载 (3) 的歧义。(自 C++11)

6) 复制构造函数。使用 other 内容的副本构造容器。

分配器是通过调用 std::allocator_traits<allocator_type>::select_on_container_copy_construction(
    other.get_allocator()) 获取的。
(自 C++11)

7) 使用 other 内容的副本构造容器,使用 alloc 作为分配器。

在 类模板参数推导 期间,只有第一个参数会影响容器的 Allocator 模板参数的推导。(自 C++23)

8) 移动构造函数。使用移动语义使用 other 的内容构造容器。分配器是通过从属于 other 的分配器移动构造得到的。移动后,保证 other 为 empty()

9) 分配器扩展的移动构造函数。使用 alloc 作为新容器的分配器,将内容从 other 移动;如果 alloc != other.get_allocator(),则会导致逐元素移动。(在这种情况下,移动后不保证 other 为空。)

在 类模板参数推导 期间,只有第一个参数会影响容器的 Allocator 模板参数的推导。(自 C++23)

10) 使用初始化列表 init 的内容构造容器。

11) 使用范围 rg 的内容构造容器。

参数

alloc用于此容器的所有内存分配的分配器
count容器的大小
value用于初始化容器元素的值
first, last范围 [firstlast),用于从中复制元素
other另一个容器,用作初始化容器元素的源
init初始化列表,用于初始化容器的元素
rg一个 容器兼容范围,即一个 input_range,其元素可转换为 T

复杂度

1,2) 常数。

3,4) 线性于 count。

5) 给定 first 和 last 之间的距离为 N,

  • 如果 first 和 last 都是前向迭代器、双向迭代器或随机访问迭代器,
  • T 的拷贝构造函数只被调用 N  次,并且
  • 不会发生重新分配。
  • 否则 (first 和 last 只是输入迭代器),
  • T 的拷贝构造函数被调用 O(N) 次,并且
  • 重新分配发生 O(log N) 次。

6,7) 线性于 other 的大小。

8) 常数。

9) 如果 alloc != other.get_allocator(),则为线性,否则为常数。

10) 线性于 init 的大小。

11) 给定 ranges::distance(rg) 为 N,

  • 如果 R 符合 ranges::forward_range 或 ranges::sized_range,
  • 将从 rg 的连续迭代器的解引用结果中初始化正好 N 个元素,并且
  • 不会发生重新分配。
  • 否则 (R 符合输入范围),
  • T 的拷贝或移动构造函数被调用 O(N) 次,并且
  • 重新分配发生 O(log N) 次。

异常

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

示例

运行此代码

#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[]{'
#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;
 
    auto const rg = {"cat", "cow", "crow"};
#ifdef __cpp_lib_containers_ranges
    std::vector<std::string> words5(std::from_range, rg); // overload (11)
#else
    std::vector<std::string> words5(rg.begin(), rg.end()); // overload (5)
#endif
    std::cout << "5: " << words5;
}
', ' ', '
#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;
 
    auto const rg = {"cat", "cow", "crow"};
#ifdef __cpp_lib_containers_ranges
    std::vector<std::string> words5(std::from_range, rg); // overload (11)
#else
    std::vector<std::string> words5(rg.begin(), rg.end()); // overload (5)
#endif
    std::cout << "5: " << words5;
}
'}; 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;   auto const rg = {"cat", "cow", "crow"}; #ifdef __cpp_lib_containers_ranges std::vector<std::string> words5(std::from_range, rg); // overload (11) #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}

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

滚动至顶部