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

news2024/9/21 16:38:48
定义于头文件 <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) 的要求。

非成员函数

按照字典顺序比较 vector 中的值

operator==,!=,<,<=,>,>=(std::vector)
template< class T, class Alloc >

bool operator==( const std::vector<T,Alloc>& lhs,

                 const std::vector<T,Alloc>& rhs );
(1)
template< class T, class Alloc >

bool operator!=( const std::vector<T,Alloc>& lhs,

                 const std::vector<T,Alloc>& rhs );
(2)
template< class T, class Alloc >

bool operator<( const std::vector<T,Alloc>& lhs,

                const std::vector<T,Alloc>& rhs );
(3)
template< class T, class Alloc >

bool operator<=( const std::vector<T,Alloc>& lhs,

                 const std::vector<T,Alloc>& rhs );
(4)
template< class T, class Alloc >

bool operator>( const std::vector<T,Alloc>& lhs,

                const std::vector<T,Alloc>& rhs );
(5)
template< class T, class Alloc >

bool operator>=( const std::vector<T,Alloc>& lhs,

                 const std::vector<T,Alloc>& rhs );
(6)

 比较二个容器的内容。

1-2) 检查 lhsrhs 的内容是否相等,即它们是否拥有相同数量的元素且 lhs 中每个元素与 rhs 的同位置元素比较相等。

3-6) 按字典序比较 lhsrhs 的内容。由等价于 std::lexicographical_compare 的函数进行比较。

参数

lhs, rhs-要比较内容的容器
- 为使用重载 (1-2) , T 必须满足可相等比较 (EqualityComparable) 的要求。
- 为使用重载 (3-6) , T 必须满足可小于比较 (LessThanComparable) 的要求。顺序关系必须建立全序。

返回值

1) 若容器内容相等则为 true ,否则为 false

2) 若容器内容不相等则为 true ,否则为 false

3) 若 lhs 的内容按字典序小于 rhs 的内容则为 true ,否则为 false

4) 若 lhs 的内容按字典序小于等于 rhs 的内容则为 true ,否则为 false

5) 若 lhs 的内容按字典序大于 rhs 的内容则为 true ,否则为 false

6) 若 lhs 的内容按字典序大于等于 rhs 的内容则为 true ,否则为 false

复杂度

1-2) 若 lhsrhs 的大小不同则为常数,否则与容器大小成线性

3-6) 与容器大小成线性

特化 std::swap 算法

std::swap(std::vector)
template< class T, class Alloc >

void swap( vector<T,Alloc>& lhs,

           vector<T,Alloc>& rhs );
(C++17 前)
template< class T, class Alloc >

void swap( vector<T,Alloc>& lhs,

           vector<T,Alloc>& rhs ) noexcept(/* see below */);
(C++17 起)

 为 std::vector 特化 std::swap 算法。交换 lhsrhs 的内容。调用 lhs.swap(rhs) 。

参数

lhs, rhs-要交换内容的容器

返回值

(无)

复杂度

常数。

异常

noexcept 规定:  

noexcept(noexcept(lhs.swap(rhs)))

(C++17 起)

调用示例

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

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;

    //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;

    //6) 复制构造函数。构造拥有 other 内容的容器。
    std::vector<Cell> vector3(vector1);
    std::cout << "vector3:  ";
    std::copy(vector3.begin(), vector3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    //1) 若容器内容相等则为 true ,否则为 false
    std::cout << "vector1 == vector2 :  " << (vector1 == vector2) << std::endl;
    std::cout << "vector1 == vector3 :  " << (vector1 == vector3) << std::endl;
    //2) 若容器内容不相等则为 true ,否则为 false
    std::cout << "vector1 != vector2 :  " << (vector1 != vector2) << std::endl;
    std::cout << "vector1 != vector3 :  " << (vector1 != vector3) << std::endl;
    //3) 若 lhs 的内容按字典序小于 rhs 的内容则为 true ,否则为 false
    std::cout << "vector1 <  vector2 :  " << (vector1 <  vector2) << std::endl;
    std::cout << "vector1 <  vector3 :  " << (vector1 <  vector3) << std::endl;
    //4) 若 lhs 的内容按字典序小于或等于 rhs 的内容则为 true ,否则为 false
    std::cout << "vector1 <= vector2 :  " << (vector1 <= vector2) << std::endl;
    std::cout << "vector1 <= vector3 :  " << (vector1 <= vector3) << std::endl;
    //5) 若 lhs 的内容按字典序大于 rhs 的内容则为 true ,否则为 false
    std::cout << "vector1 >  vector2 :  " << (vector1 >  vector2) << std::endl;
    std::cout << "vector1 >  vector3 :  " << (vector1 >  vector3) << std::endl;
    //6) 若 lhs 的内容按字典序大于或等于 rhs 的内容则为 true ,否则为 false
    std::cout << "vector1 >= vector2 :  " << (vector1 >= vector2) << std::endl;
    std::cout << "vector1 >= vector3 :  " << (vector1 >= vector3) << std::endl;
    std::cout << std::endl;

    //为 std::vector 特化 std::swap 算法。
    //交换 lhs 与 rhs 的内容。调用 lhs.swap(rhs) 。
    std::cout << "swap before:" << std::endl;
    std::cout << "vector1:  ";
    std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << "vector2:  ";
    std::copy(vector2.begin(), vector2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    vector1.swap(vector2);
    std::cout << "swap after:" << std::endl;
    std::cout << "vector1:  ";
    std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << "vector2:  ";
    std::copy(vector2.begin(), vector2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    return 0;
}

 输出

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/509222.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

单元测试 - 注解篇

1. RunWith 指定单测的运行环境 RunWith(JUnit4.class) - JUnit4环境RunWith(MockitoJUnitRunner.class) - Mock环境RunWith(SpringJUnit4ClassRunner.class) / RunWith(SpringRunner.class) - Spring环境 ps: SpringJUnit4ClassRunner 与 SpringRunner区别 SpringRunner继承…

Gradio的web界面演示与交互机器学习模型,分享应用《3》

Gradio的web界面演示与交互机器学习模型&#xff0c;安装和使用《1》https://blog.csdn.net/weixin_41896770/article/details/130540360Gradio的web界面演示与交互机器学习模型&#xff0c;主要特征《2》https://blog.csdn.net/weixin_41896770/article/details/130556692 前…

pywinauto使用教程

这里写自定义目录标题 引入pycharm项目新的改变功能快捷键合理的创建标题&#xff0c;有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants 创建一个自定义列表如何创建一个注脚注…

JDK的版本迭代(JDK9 - JDK20)

文章目录 1. 发布特点2. 名词解释Oracle JDK和Open JDKJEPLTS 3. 各版本支持时间路线图4. 各版本介绍jdk 9jdk 10jdk 11jdk 12jdk 13jdk 14jdk 15jdk 16jdk 17jdk 18jdk 19jdk 20 5. JDK各版本下载链接6. 应该如何学习新特性 1. 发布特点 发行版本发行时间备注Java 1.01996.01…

[Java]JavaWeb学习笔记(动力节点老杜2022)【Javaweb+MVC架构模式完结】

文章目录 &#x1f97d; 视频对应资料&#x1f97d; Tomcat服务器&#x1f30a; 下载与安装&#x1f30a; 关于Tomcat服务器的目录&#x1f30a; 启动Tomcat&#x1f30a; 实现一个最基本的web应用&#xff08;这个web应用中没有java小程序&#xff09; &#x1f97d; 静态资源…

(附源码)springboot学生宿舍管理系统 毕业设计 211955

摘 要 科技进步的飞速发展引起人们日常生活的巨大变化&#xff0c;电子信息技术的飞速发展使得电子信息技术的各个领域的应用水平得到普及和应用。信息时代的到来已成为不可阻挡的时尚潮流&#xff0c;人类发展的历史正进入一个新时代。在现实运用中&#xff0c;应用软件的工作…

人工智能AI到底能AI到什么程度?

作为引领新一轮科技革命和产业变革的重要驱动力&#xff0c;人工智能催生了大批新产品、新技术、新业态和新模式。日前&#xff0c;全新的聊天机器人模型ChatGPT因其强大的语言理解和文本生成能力&#xff0c;引发自“阿尔法狗”后大众对人工智能的第二波关注高潮。 据悉&…

Java笔记_14(集合进阶2)

Java笔记_14 一、双列集合1.1、Map的常见API1.2、Map遍历方式一&#xff08;键找值&#xff09;1.3、Map集合遍历方法二&#xff08;键值对&#xff09;1.4、Map集合遍历方法三&#xff08;lambda表达式&#xff09;1.5、HashMap1.6、HashMap练习1.7、HashMap底层源码解析1.8、…

【ChatGPT】ChatGPT+飞书,打造智能问答助手

Yan-英杰的主页 悟已往之不谏 知来者之可追 C程序员&#xff0c;2024届电子信息研究生 目录 前言 环境列表 视频教程 1.飞书设置 2.克隆feishu-chatgpt项目 3.配置config.yaml文件 4.运行feishu-chatgpt项目 5.安装cpolar内网穿透 6.固定公网地址 7.机器人权限配…

Unity本地化:添加多语言支持

文档&#xff1a;Quick Start Guide | Localization | 1.2.1 (unity3d.com) /**************************************************** 文件&#xff1a;LocaleSelector.cs 作者&#xff1a;Edision 日期&#xff1a;#CreateTime# 功能&#xff1a;语言本地化 *…

vue脚手架(vue-cli)详细安装过程

CLI&#xff0c;俗称脚手架。全称是Command Line Interface。 vue-cli 是vue官方发布的开发vue项目的脚手架。 vue脚手架用于自动生成vue和webpack的项目模板&#xff0c;是一个快速构建vue项目的工具&#xff0c;可以自动安装vue所需要的插件&#xff0c;避免手动安装各种插件…

camunda执行监听器和任务监听器有什么区别

Camunda的执行监听器和任务监听器是用于添加自定义逻辑的监听器&#xff0c;它们的区别在于作用对象和触发事件的不同。 执行监听器是与BPMN流程中的各种流程元素&#xff08;例如开始事件、用户任务、服务任务、网关等&#xff09;相关联的。执行监听器可以在流程元素执行前、…

德邦快递:逆境之下,让数字化辅助业务的利润增长

#01行业背景 2022年&#xff0c;我国快递业务量完成 1105.8 亿件&#xff0c;业务量连续 9 年位居世界第一&#xff0c;仅用七年时间&#xff0c;中国的快递行业就完成了从百亿到千亿的十倍增长。我国快递物流行业正从蓝海进入红海&#xff0c;在下半场激烈竞争中破局的关键在…

线上问题-CPU使用频率飙升

描述 中午收到群内人员反馈环境访问速度慢。登录验证码打不开等问题。通过查看日志发现是kafka出现问题&#xff0c;无法处理消息。联系运维解决。在排查的过程中使用mobaXterm连接服务器。左下角看到CPU使用频率非常高。于是记录一下通过CPU查看程序占用情况分析问题。 过程 …

各大厂与卡顿和ANR的战斗记录篇

作者&#xff1a;Drummor 1.1 认识ANR 1.1.1 系统如何处理ANR 设计原理和影响因素篇&#xff0c;主要对以下关键问题展开 ANR触发的条件以及根本原因发生ANR之后&#xff0c;系统处理ANR的流程。应用层如何判定ANR&#xff1a;对ANR的感知&#xff0c;通过监听SIGQUIT信号。…

直播合辑 | 微软ATP与您相约100场公益演讲

&#xff08;本文阅读时间&#xff1a;5 分钟&#xff09; Public100已历经了近一年的春夏秋冬&#xff0c;截止目前我们一共举办33场公益直播&#xff0c;由微软及合作伙伴中从事 AI 相关工作的工程师、产品经理、市场总监、运营经理等各类专家和学者&#xff0c;分享自己在学…

IPC机制之管道

每个进程各自有不同的用户地址空间&#xff0c;任何一个进程的全局变量在另一个进程中都看不到&#xff0c;所以进程之间要交换数据必须通过内核&#xff0c;在内核中开辟一块缓冲区&#xff0c;进程1把数据从用户空间拷到内核缓冲区&#xff0c;进程2再从内核缓冲区把数据读走…

一行代码绘制高分SCI火山图

一、概述 在近半年中&#xff0c;我读了很多的高分SCI文章&#xff0c;很多文章中都有多种不同的火山图&#xff0c;包括「普通的火山图、渐变火山图、以及包含GO通路信息的火山图」&#xff01; 经过一段时间的文献阅读和资料查询&#xff0c;终于找到了一个好用而且简单的包…

烽火HG680KA-Hi3798MV300-当贝纯净桌面-卡刷固件包

烽火HG680KA-Hi3798MV300-当贝纯净桌面-卡刷固件包-内有教程 特点&#xff1a; 1、适用于对应型号的电视盒子刷机&#xff1b; 2、开放原厂固件屏蔽的市场安装和u盘安装apk&#xff1b; 3、修改dns&#xff0c;三网通用&#xff1b; 4、大量精简内置的没用的软件&#xff…

乳杆菌属Lactobacillus——维持肠道和阴道健康不可忽缺的角色

谷禾健康 乳杆菌属&#xff08;Lactobacillus&#xff09;是厚壁菌门乳杆菌科下的一类革兰氏阳性菌&#xff0c;最早于19世纪在酸奶中发现。 乳杆菌在自然界中分布很广&#xff0c;在植物体表、乳制品、肉制品、葡萄酒、发酵面团、污水以及人畜粪便中&#xff0c;均可分离到。在…