栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ STL vector构造函数

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ STL vector构造函数

文章目录
  • 一、 定义
    • 复杂度:

一、 定义
std::vector::vector

1
vector();
vector() noexcept(noexcept(Allocator()));
constexpr vector() noexcept(noexcept(Allocator()));

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


2
explicit vector( const Allocator& alloc );

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


3
constexpr vector( size_type count,
                  const T& value,
                  const Allocator& alloc = Allocator());

使用具有值 value 元素的 count 个副本构造容器。


4
explicit vector( size_type count, const Allocator& alloc = Allocator() );

使用 count 个默认插入的 T 实例构造容器。不制作副本


5
template< class InputIt >
vector( InputIt first, InputIt last,
        const Allocator& alloc = Allocator() );

使用范围 [ first,last ] 的内容构造容器。
如果InputIt 是整数类型的话,则与下面的构造函数具有相同效果

vector(static_cast(first), static_cast(last), a)

6
constexpr vector( const vector& other );

复制构造函数。使用 other 内容的副本构造容器。
分配器是通过调用获下面的函数获得的:

std::allocator_traits::select_on_container_copy_construction(
    other.get_allocator())

7
constexpr vector( const vector& other, const Allocator& alloc );

使用 other 的内容副本构造容器,使用 alloc 作为分配器。
模板参数 Allocator 在类模板参数推导中使用时仅从第一个参数推导。


8
constexpr vector( vector&& other ) noexcept;

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

**具体使用语法:**用 std::move() 函数转,或者某个函数的返回值是 vector 容器,返回值也是一个右值。

vector vt(2, 10);
//move是为了转移所有权,将快要销毁的对象转移给其他变量,这样可以继续使用这个对象,而不必再创建一个一样的对象。省去了创建新的一样内容的对象,也就提高了性能。
vector fe(move(vt));

9
constexpr vector( vector&& other, const Allocator& alloc );

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


10
constexpr vector( std::initializer_list init,
                  const Allocator& alloc = Allocator() );

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


上面函数中的参数说明:

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


复杂度:

(1、2):常量
(3、4):线性计数
(5):第一个和最后一个之间的距离成线性
(6、7):与其他的大小成线性
(8):常量
(9):如果 alloc != other.get_allocator() 则为线性,否则为常量。
(10):与 init 的大小成线性关系


示例代码:

#include 
#include 
#include 
 
template
std::ostream& operator<<(std::ostream& s, const std::vector& v) 
{
    s.put('[');
    char comma[3] = {'', ' ', ''};
    for (const auto& e : v) {
        s << comma << e;
        comma[0] = ',';
    }
    return s << ']';
}
 
int main() 
{
    // 构造函数语法
    std::vector words1 {"the", "frogurt", "is", "also", "cursed"};
    std::cout << "words1: " << words1 << 'n';
 
    // words2 == words1
    std::vector words2(words1.begin(), words1.end());
    std::cout << "words2: " << words2 << 'n';
 
    // words3 == words1
    std::vector words3(words1);
    std::cout << "words3: " << words3 << 'n';
 
    // words4 is {"Mo", "Mo", "Mo", "Mo", "Mo"}
    std::vector 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]
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/443863.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号