c++ 11标准模板(STL) std::vector (二)

这篇具有很好参考价值的文章主要介绍了c++ 11标准模板(STL) std::vector (二)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

定义于头文件 <vector>
template<

    class T,
    class Allocator = std::allocator<T>

> class vector;
(1)
namespace pmr {

    template <class T>
    using vector = std::vector<T, std::pmr::polymorphic_allocator<T>>;

}
(2) (C++17 起)

1) std::vector 是封装动态数组的顺序容器。

2) std::pmr::vector 是使用多态分配器的模板别名。

元素相继存储,这意味着不仅可通过迭代器,还能用指向元素的常规指针访问元素。这意味着指向 vector 元素的指针能传递给任何期待指向数组元素的指针的函数。

(C++03 起)

vector 的存储是自动管理的,按需扩张收缩。 vector 通常占用多于静态数组的空间,因为要分配更多内存以管理将来的增长。 vector 所用的方式不在每次插入元素时,而只在额外内存耗尽时重分配。分配的内存总量可用 capacity() 函数查询。额外内存可通过对 shrink_to_fit() 的调用返回给系统。 (C++11 起)

重分配通常是性能上有开销的操作。若元素数量已知,则 reserve() 函数可用于消除重分配。

vector 上的常见操作复杂度(效率)如下:

  • 随机访问——常数 O(1)
  • 在末尾插入或移除元素——均摊常数 O(1)
  • 插入或移除元素——与到 vector 结尾的距离成线性 O(n)

std::vector (对于 bool 以外的 T )满足容器 (Container) 、具分配器容器 (AllocatorAwareContainer) 、序列容器 (SequenceContainer) 、连续容器 (ContiguousContainer) (C++17 起)及可逆容器 (ReversibleContainer) 的要求。

 文章来源地址https://www.toymoban.com/news/detail-431668.html

成员函数

构造 vector

std::vector<T,Allocator>::vector

vector();

(1) (C++17 前)

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

(C++17 起)

explicit vector( const Allocator& alloc );

(2) (C++17 前)

explicit vector( const Allocator& alloc ) noexcept;

(C++17 起)
explicit vector( size_type count,

                 const T& value = T(),

                 const Allocator& alloc = Allocator());
(3) (C++11 前)
         vector( size_type count,

                 const T& value,

                 const Allocator& alloc = Allocator());
(C++11 起)

explicit vector( size_type count );

(4) (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 起)

vector( vector&& other );

(7) (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 的分配器移动构造获得。移动后,保证 other 为 empty() 。

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 。

 

析构 vector

std::vector<T,Allocator>::~vector

~vector();

销毁容器。调用元素的析构函数,然后解分配所用的存储。注意,若元素是指针,则不销毁所指向的对象。

复杂度

与容器大小成线性。

 

调用示例

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <time.h>
#include <vector>

using namespace std;

struct Cell
{
    int x;
    int y;

    Cell() = default;
    Cell(int a, int b): x(a), y(b) {}

    Cell &operator +=(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator +(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator *(const Cell &cell)
    {
        x *= cell.x;
        y *= cell.y;
        return *this;
    }

    Cell &operator ++()
    {
        x += 1;
        y += 1;
        return *this;
    }


    bool operator <(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y < cell.y;
        }
        else
        {
            return x < cell.x;
        }
    }

    bool operator >(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y > cell.y;
        }
        else
        {
            return x > cell.x;
        }
    }

    bool operator ==(const Cell &cell) const
    {
        return x == cell.x && y == cell.y;
    }
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}


int main()
{
    std::cout << std::boolalpha;

    std::mt19937 g{std::random_device{}()};
    srand((unsigned)time(NULL));

    auto generate = []()
    {
        int n = std::rand() % 10 + 110;
        Cell cell{n, n};
        return cell;
    };

    //从各种数据源构造新容器,可选地使用用户提供的分配器 alloc 。
    //1) 默认构造函数。构造拥有默认构造的分配器的空容器。
    //2) 构造拥有给定分配器 alloc 的空容器。
    std::vector<Cell> vector1;
    std::cout << "vector1   empty: " << vector1.empty() << std::endl;

    //3) 构造拥有 count 个有值 value 的元素的容器。
    std::vector<Cell> vector2(6, generate());
    std::cout << "vector2:  ";
    std::copy(vector2.begin(), vector2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    //4) 构造拥有个 count 默认插入的 T 实例的容器。不进行复制。
    std::vector<Cell> vector3(6);
    std::cout << "vector3:  ";
    std::copy(vector3.begin(), vector3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    //5) 构造拥有范围 [first, last) 内容的容器。
    //6) 复制构造函数。构造拥有 other 内容的容器。若不提供 alloc ,则如同通过调用获得分配器
    std::vector<Cell> vector4(vector2.begin(), vector2.end());
    std::cout << "vector4:  ";
    std::copy(vector4.begin(), vector4.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    //7) 移动构造函数。用移动语义构造拥有 other 内容的容器。
    //分配器通过属于 other 的分配器移动构造获得。移动后,保证 other 为 empty() 。
    //8) 有分配器扩展的移动构造函数。以 alloc 为新容器的分配器,
    //从 other 移动内容;若 alloc != other.get_allocator() ,则它导致逐元素移动。
    std::vector<Cell> vector5(std::move(vector2));
    std::cout << "vector5:  ";
    std::copy(vector5.begin(), vector5.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << "vector2   empty: " << vector2.empty() << std::endl;

    //9) 构造拥有 initializer_list init 内容的容器。
    std::vector<Cell> vector6({{101, 101}, {102, 102}, {103, 103},
        {104, 104}, {105, 105}, {106, 106}});
    std::cout << "vector6:  ";
    std::copy(vector6.begin(), vector6.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    return 0;
}

输出

c++ 11标准模板(STL) std::vector (二)

 

到了这里,关于c++ 11标准模板(STL) std::vector (二)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • c++11 标准模板(STL)(std::basic_istream)(三)

    c++11 标准模板(STL)(std::basic_istream)(三)

    template     class CharT,     class Traits = std::char_traitsCharT class basic_istream : virtual public std::basic_iosCharT, Traits  类模板 basic_istream 提供字符流上的高层输入支持。受支持操作包含带格式的输入(例如整数值或空白符分隔的字符与字符串)和无格式输入(例如未处理字符和字符数组)

    2024年02月13日
    浏览(7)
  • c++11 标准模板(STL)(std::basic_ostream)(八)

    c++11 标准模板(STL)(std::basic_ostream)(八)

    template     class CharT,     class Traits = std::char_traitsCharT class basic_ostream : virtual public std::basic_iosCharT, Traits 类模板 basic_ostream 提供字符流上的高层输出操作。受支持操作包含有格式输出(例如整数值)和无格式输出(例如生字符和字符数组)。此功能以 basic_streambuf 类所提供的接

    2024年02月12日
    浏览(10)
  • c++11 标准模板(STL)(std::basic_istream)(一)

    c++11 标准模板(STL)(std::basic_istream)(一)

    template     class CharT,     class Traits = std::char_traitsCharT class basic_istream : virtual public std::basic_iosCharT, Traits 类模板 basic_istream 提供字符流上的高层输入支持。受支持操作包含带格式的输入(例如整数值或空白符分隔的字符与字符串)和无格式输入(例如未处理字符和字符数组)。

    2024年02月13日
    浏览(7)
  • c++11 标准模板(STL)(std::unordered_multimap)(九)

    template     class Key,     class T,     class Hash = std::hashKey,     class KeyEqual = std::equal_toKey,     class Allocator = std::allocator std::pairconst Key, T class unordered_multimap; (1) (C++11 起) namespace pmr {     template class Key, class T,               class Hash = std::hashKey,               class Pred = std::equa

    2023年04月09日
    浏览(11)
  • c++11 标准模板(STL)(std::basic_stringstream)(一)

    c++11 标准模板(STL)(std::basic_stringstream)(一)

    template     class CharT,     class Traits = std::char_traitsCharT class basic_stringstream; (C++11 前) template     class CharT,     class Traits = std::char_traitsCharT,     class Allocator = std::allocatorCharT class basic_stringstream; (C++11 起) 类模板 std::basic_stringstream 实现基于字符串的流上的输入与输出操作。它等效

    2024年02月10日
    浏览(10)
  • c++11 标准模板(STL)(std::priority_queue)(四)

    c++11 标准模板(STL)(std::priority_queue)(四)

    template     class T,     class Container = std::vectorT,     class Compare = std::lesstypename Container::value_type class priority_queue; priority_queue 是容器适配器,它提供常数时间的(默认)最大元素查找,对数代价的插入与释出。 可用用户提供的 Compare 更改顺序,例如,用 std::greaterT 将导致最小元

    2024年02月01日
    浏览(10)
  • c++11 标准模板(STL)(std::basic_stringstream)(四)

    c++11 标准模板(STL)(std::basic_stringstream)(四)

    template     class CharT,     class Traits = std::char_traitsCharT class basic_stringstream; (C++11 前) template     class CharT,     class Traits = std::char_traitsCharT,     class Allocator = std::allocatorCharT class basic_stringstream; (C++11 起) 类模板 std::basic_stringstream 实现基于字符串的流上的输入与输出操作。它等效

    2024年02月10日
    浏览(12)
  • c++11 标准模板(STL)(std::basic_ifstream)(一)

    c++11 标准模板(STL)(std::basic_ifstream)(一)

    定义于头文件  fstream template     class CharT,     class Traits = std::char_traitsCharT class basic_ifstream : public std::basic_istreamCharT, Traits 类模板 basic_ifstream 实现文件流上的高层输入操作。它将 std::basic_istream 的高层接口赋予基于文件的流缓冲( std::basic_filebuf )。 std::basic_ifstream 的典型实

    2024年02月14日
    浏览(13)
  • 16 标准模板库STL之vector

    基础知识         1、vector和数组有点类似,但它比数组更好用。一般来说,数组的长度是不能动态拓展的,因此就需要考虑长度到底多大合适。长度不能过大,否则浪费内存;也不能过小,否则内存不够。vector正好弥补了这个缺陷,相当于一个可以自动改变数组长度的动

    2023年04月17日
    浏览(9)
  • STL标准模板库 vector容器与迭代器入门

    STL标准模板库 vector容器与迭代器入门

    vector 就是一个连续的数据 C++11 std::vector a ={1,4,2,6,7}; 可以使用花括号来定义 容器的功能就是存储数据 迭代器的功能就是指向数据,并且可以实现前后移动(指针)算法和容器的接口的存在 vector功能是长度可变的数组, 身在栈上 里面的数据存储在堆上 因为栈不可动态扩容

    2023年04月23日
    浏览(18)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包