定义于头文件 <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>::begin,
std::vector<T,Allocator>::cbegin

iterator begin();

(C++11 前)

iterator begin() noexcept;

(C++11 起)

const_iterator begin() const;

(C++11 前)

const_iterator begin() const noexcept;

(C++11 起)

const_iterator cbegin() const noexcept;

(C++11 起)

返回指向容器首元素的迭代器。

若容器为空,则返回的迭代器将等于 end() 。

参数

(无)

返回值

指向首元素的迭代器。

复杂度

常数。

返回指向容器尾端的迭代器

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

iterator end();

(C++11 前)

iterator end() noexcept;

(C++11 起)

const_iterator end() const;

(C++11 前)

const_iterator end() const noexcept;

(C++11 起)

const_iterator cend() const noexcept;

(C++11 起)

返回指向容器末元素后一元素的迭代器。

此元素表现为占位符;试图访问它导致未定义行为。

参数

(无)

返回值

指向后随最后元素的迭代器。

复杂度

常数。

返回指向容器最后元素的逆向迭代器

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

reverse_iterator rbegin();

(C++11 前)

reverse_iterator rbegin() noexcept;

(C++11 起)

const_reverse_iterator rbegin() const;

(C++11 前)

const_reverse_iterator rbegin() const noexcept;

(C++11 起)

const_reverse_iterator crbegin() const noexcept;

(C++11 起)

返回指向逆向容器首元素的逆向迭代器。它对应非逆向容器的末元素。

参数

(无)

返回值

指向首元素的逆向迭代器。

复杂度

常数。

返回指向前端的逆向迭代器

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

reverse_iterator rend();

(C++11 前)

reverse_iterator rend() noexcept;

(C++11 起)

const_reverse_iterator rend() const;

(C++11 前)

const_reverse_iterator rend() const noexcept;

(C++11 起)

const_reverse_iterator crend() const noexcept;

(C++11 起)

返回指向逆向容器末元素后一元素的逆向迭代器。它对应非逆向容器首元素的前一元素。此元素表现为占位符,试图访问它导致未定义行为。

参数

(无)

返回值

指向末元素后一元素的逆向迭代器。

复杂度

常数。

调用示例

#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;//返回指向容器首元素的迭代器。若容器为空,则返回的迭代器将等于 end() 。//返回指向容器末元素后一元素的迭代器。此元素表现为占位符;试图访问它导致未定义行为。for (std::vector<Cell>::iterator it = vector1.begin(); it != vector1.end(); it++){Cell cell = generate();*it = cell;std::cout << "iterator: " << &(*it) << " = " << cell << std::endl;}std::cout << std::endl;for (std::vector<Cell>::const_iterator cit = vector1.cbegin(); cit != vector1.cend(); cit++){std::cout << "const_iterator: " << &(*cit) << " : " << *cit << std::endl;}std::cout << std::endl;//返回指向逆向容器首元素的逆向迭代器。它对应非逆向容器的末元素。//返回指向逆向容器末元素后一元素的逆向迭代器。//它对应非逆向容器首元素的前一元素。此元素表现为占位符,试图访问它导致未定义行为。for (std::vector<Cell>::reverse_iterator rit = vector1.rbegin(); rit != vector1.rend(); rit++){Cell cell = generate();*rit = cell;std::cout << "reverse_iterator: " << &(*rit) << " = " << cell << std::endl;}std::cout << std::endl;for (std::vector<Cell>::const_reverse_iterator crit = vector1.crbegin(); crit != vector1.crend(); crit++){std::cout << "const_reverse_iterator: " << &(*crit) << " : " << *crit << std::endl;}std::cout << std::endl;return 0;
}

输出

c++ 11标准模板(STL) std::vector (五)相关推荐

  1. C++中标准模板库std::vector的实现

    以下实现了C++标准模板库std::vector的部分实现,参考了 cplusplus. 关于C++中标准模板库std::vector的介绍和用法可以参考 https://blog.csdn.net/ ...

  2. c++ 11标准模板(STL) std::vector (八)

    定义于头文件 <vector> template< class T,     class Allocator = std::allocator<T> > class ...

  3. c++ 11标准模板(STL) std::vector (四)

    定义于头文件 <vector> template< class T,     class Allocator = std::allocator<T> > class ...

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

    定义于头文件 <vector> template< class T,     class Allocator = std::allocator<T> > class ...

  5. C++11标准模板(STL)- 算法(std::set_symmetric_difference)

    定义于头文件 <algorithm> 算法库提供大量用途的函数(例如查找.排序.计数.操作),它们在元素范围上操作.注意范围定义为 [first, last) ,其中 last 指代要查询 ...

  6. C++11标准模板(STL)- 算法 - 集合操作(在已排序范围上)(std::set_difference)

    定义于头文件 <algorithm> 算法库提供大量用途的函数(例如查找.排序.计数.操作),它们在元素范围上操作.注意范围定义为 [first, last) ,其中 last 指代要查询 ...

  7. C++中标准模板库std::pair的实现

    以下用C++实现了标准模板库中的std::pair实现,参考了 cplusplus 和 vs2013中的utility文件. 关于std::pair的介绍和用法可以参考: https://blog.c ...

  8. C++11标准模板(STL)- 算法(std::nth_element)

    定义于头文件 <algorithm> 算法库提供大量用途的函数(例如查找.排序.计数.操作),它们在元素范围上操作.注意范围定义为 [first, last) ,其中 last 指代要查询 ...

  9. C++11标准模板(STL)- 算法(std::adjacent_difference)

    定义于头文件 <algorithm> 算法库提供大量用途的函数(例如查找.排序.计数.操作),它们在元素范围上操作.注意范围定义为 [first, last) ,其中 last 指代要查询 ...

最新文章

  1. TokuDB · 引擎特性 · HybridDB for MySQL高压缩引擎TokuDB 揭秘
  2. 关于判断checkbox选中问题
  3. Java 对象都是在堆上分配内存吗?
  4. ant target间的dependency
  5. matlab变量由非标量,matlab中的if语句
  6. Android SpannableString
  7. 三星s8android pie,三星更改Galaxy S8的Android Pie更新计划,添加Gala
  8. 【翻译自mos文章】rman 备份时报:ORA-02396: exceeded maximum idle time
  9. 《混合云计算》——2.3 整合势在必行
  10. 注册登录时本地图片验证码
  11. java虚拟机的heap监狱_JVM垃圾回收--垃圾收集器总结
  12. python音乐下载器
  13. 魅族17用鸿蒙系统,发布一年还很香,魅族17升级Flyme 9后太好用了
  14. librosa.stft librosa.core.stft
  15. 站内搜索引擎(ASP.NET)
  16. asp.net 使用UrlRewritingNet.UrlRewriter组件URL重写,伪静态详解
  17. 计算机交换机配置实验心得,网络配置实验心得
  18. C/C++就业领域与学习方向 | 学习计划
  19. 网站改版会影响网站流量吗
  20. 顶级测试框架Jest指南:跑通一个完美的程序,就是教出一群像样的学生

热门文章

  1. C++:实现量化Convertible Bonds可转换债券测试实例
  2. 看了就知道世纪佳缘为什么能够上市了,这个市场的确够大啊
  3. NTKO OFFICE文档控件的卸载和安装
  4. 【Python练习】如何使用Pandas获取“豆瓣电影”相关数据,并生成Excel表格
  5. 数据结构课程设计_《旅游景区信息管理系统》
  6. IPv6+:构筑云网基石,共倡智联未来
  7. 名风SEO百度点击软件
  8. 【产品实战-乘风游旅游App】1.0 自驾游市场调研
  9. tf.zeros_initializer
  10. JS的Form表单转JSON格式