当前位置: 首页 > article >正文

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) 的要求。

元素访问

访问指定的元素,同时进行越界检查

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

reference       at( size_type pos );

const_reference at( size_type pos ) const;

 返回位于指定位置 pos 的元素的引用,有边界检查。

pos 不在容器范围内,则抛出 std::out_of_range 类型的异常。

参数

pos-要返回的元素的位置

返回值

到所需元素的引用。

异常

若 !(pos < size()) 则抛出 std::out_of_range

复杂度

常数。

访问指定的元素

std::vector<T,Allocator>::operator[]

reference       operator[]( size_type pos );

const_reference operator[]( size_type pos ) const;

 返回位于指定位置 pos 的元素的引用。不进行边界检查。

参数

pos-要返回的元素的位置

返回值

到所需元素的引用。

复杂度

常数。

注意

不同于 std::map::operator[] ,此运算符决不插入新元素到容器。

访问第一个元素

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

reference front();

const_reference front() const;

 返回到容器首元素的引用。

在空容器上对 front 的调用是未定义的。

参数

(无)

返回值

到首元素的引用

复杂度

常数

注意

对于容器 c ,表达式 c.front() 等价于 *c.begin() 。

访问最后一个元素

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

reference back();

const_reference back() const;

 返回到容器中最后一个元素的引用。

在空容器上对 back 的调用是未定义的。

参数

(无)

返回值

到最后元素的引用。

复杂度

常数。

注意

对于容器 c ,表达式 return c.back(); 等价于 { auto tmp = c.end(); --tmp; return *tmp; }

返回指向内存中数组第一个元素的指针

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

T* data() noexcept;

(C++11 起)

const T* data() const noexcept;

(C++11 起)

 返回指向作为元素存储工作的底层数组的指针。指针满足范围 [data(); data() + size()) 始终是合法范围,即使容器为空(该情况下 data() 不可解引用)。

参数

(无)

返回值

指向底层元素存储的指针。对于非空容器,返回的指针与首元素地址比较相等。

复杂度

常数。

注意

若 size() 为 0 ,则 data() 可能或可能不返回空指针。

调用示例

#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;
    };


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

    for (size_t index = 0 ; index < vector1.size(); index++)
    {
        Cell cell = generate();
        //返回位于指定位置 pos 的元素的引用,有边界检查。
        //若 pos 不在容器范围内,则抛出 std::out_of_range 类型的异常。
        //reference       at( size_type pos );
        vector1.at(index) = cell;
        std::cout << "vector1.at( " << index << " ) = " << cell << std::endl;
    }
    std::cout << std::endl;

    for (size_t index = 0 ; index < vector1.size(); index++)
    {
        //返回位于指定位置 pos 的元素的引用,有边界检查。
        //若 pos 不在容器范围内,则抛出 std::out_of_range 类型的异常。
        //const_reference at( size_type pos ) const;
        std::cout << "vector1.at( " << index << " ) = "
                  << vector1.at(index) << std::endl;
    }
    std::cout << std::endl;

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


    for (size_t index = 0 ; index < vector2.size(); index++)
    {
        Cell cell = generate();
        //返回位于指定位置 pos 的元素的引用。不进行边界检查。
        //reference       operator[]( size_type pos );
        vector2[index] = cell;
        std::cout << "vector2[] " << index << " ] = " << cell << std::endl;
    }
    std::cout << std::endl;

    for (size_t index = 0 ; index < vector2.size(); index++)
    {
        //返回位于指定位置 pos 的元素的引用。不进行边界检查。
        //const_reference operator[]( size_type pos ) const;
        std::cout << "vector2[ " << index << " ] = "
                  << vector2[index] << std::endl;
    }
    std::cout << std::endl;

    //返回到容器首元素的引用。在空容器上对 front 的调用是未定义的。
    Cell cell = generate();
    vector1.front() = cell;
    std::cout << "vector1.front() = " << cell << std::endl;
    std::cout << "vector1.front() : " << vector1.front() << std::endl;
    std::cout << std::endl;

    //返回到容器中最后一个元素的引用。在空容器上对 back 的调用是未定义的。
    vector1.back() = cell;
    std::cout << "vector1.back()  = " << cell << std::endl;
    std::cout << "vector1.back()  : " << vector1.back() << std::endl;
    std::cout << std::endl;

    for (size_t index = 0; index < vector1.size(); index++)
    {
        //返回指向作为元素存储工作的底层数组的指针。
        //指针满足范围 [data(); data() + size()) 始终是合法范围,
        //即使容器为空(该情况下 data() 不可解引用)。
        //T* data() noexcept;
        Cell cell = generate();
        *(vector1.data() + index) = cell;
        std::cout << "vector1: " << (vector1.data() + index)
                  << " = " << cell << std::endl;
    }
    std::cout << std::endl;

    for (size_t index = 0; index < vector1.size(); index++)
    {
        //返回指向作为元素存储工作的底层数组的指针。
        //指针满足范围 [data(); data() + size()) 始终是合法范围,
        //即使容器为空(该情况下 data() 不可解引用)。
        //const T* data() const noexcept;
        std::cout << "vector1: " << (vector1.data() + index) << " : "
                  << *(vector1.data() + index) << std::endl;
    }

    return 0;
}

输出

 


http://www.kler.cn/news/16343.html

相关文章:

  • Node服务端开发【NPM】
  • USB转串口芯片CH9101U
  • 当一个测试人员说他“测完了”,里面的坑是什么?
  • [创新工具和方法论]-02- DOE实验设计步骤
  • Adobe Photoshop 软件下载
  • 网络基础:socket套接字
  • 极客之眼 Nmap:窥探世界的第一步
  • chatGPT国内可用镜像源地址
  • 商城管理系统的数据表从属关系+navicat建表操作+数据库文件转储并入代码操作
  • 改进YOLOv5/YOLOv8:(创新必备)全新注意力机制DAED-Conv | 高效轻量化注意力下采样 | 大幅降低参数量的同时增加模型精度。
  • 嗯,这个树怎么和往常不一样?
  • 一篇文章让你彻底掌握 Shell
  • [架构之路-178]-《软考-系统分析师》-17-嵌入式系统分析与设计- 3- 分区操作系统(Partition Operating System)概述
  • 【微机原理】8088/8086CPU引脚
  • 基于SSM+SpringBoot+Vue的快递物流仓库管理系统
  • 5、产品运营 - 产品管理系列文章
  • 【网络原理】网络通信与协议
  • Mix.AI.How is your Chirper?
  • Shell编程之正则表达式
  • 干货 | 赵亚雄:大模型、AI经济和AI基础设施
  • 线性表之单链表(详解)
  • c语言和cpp里面的强制类型转换
  • 嵌入式linux学习笔记--虚拟局域网组网方案分享,基于自组zerotier -planet 网络的方案
  • RabbitMQ面试题
  • Linux系统操作案例-配置Nginx的负载均衡与转发
  • 110页智慧农业解决方案(农业信息化解决方案)(ppt可编辑)
  • Spring 5 笔记 - AOP
  • 【python知识】__init__.py的来龙去脉
  • 【Mysql】基础篇:DML(data manipulation language)语句:增、删、改数据库数据总结
  • pod之debug初始化容器