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

【C++】vector

目录

vector

1. vector的成员函数

1.1 构造、析构和赋值运算符重载

1.1.1 构造函数

1.1.2 析构函数

1.1.3 赋值运算符重载

1.2 迭代器

1.3 容量

1.4 元素访问

1.4.1 遍历方法

1.5 修改器

1.6 配置器

2. vector的非成员函数


vector

1. vector的成员函数

1.1 构造、析构和赋值运算符重载

1.1.1 构造函数

default

explicit vector(const allocator_type& alloc = allocator_type());默认构造
fillexplicit vector(size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type());用n个val来构造
rangetemplate <class InputIterator>         vector(InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());用迭代器区间构造

copy

vector(const vector& x);

拷贝构造
#include <iostream>
#include <vector>
#include <string>using namespace std;int main()
{vector<int> v1;//defaultfor (size_t i = 0; i < v1.size(); ++i){cout << v1[i] << " ";}cout << endl;//空vector<int> v2(10, 1);//fillfor (size_t i = 0; i < v2.size(); ++i){cout << v2[i] << " ";}cout << endl;//1 1 1 1 1 1 1 1 1 1string s("hello world");vector<char> v3(s.begin() + 3, --s.end());//rangefor (size_t i = 0; i < v3.size(); ++i){cout << v3[i] << " ";}cout << endl;//l o   w o r lvector<char> v4(v3);//copyfor (size_t i = 0; i < v4.size(); ++i){cout << v4[i] << " ";}cout << endl;//l o   w o r lreturn 0;
}

1.1.2 析构函数

~vector();销毁vector类对象

1.1.3 赋值运算符重载

copyvector& operator= (const vector& x);

1.2 迭代器

begin&end    rbegin&rend    cbegin&cend    crbegin&crend

与string的迭代器类似,详见【C++】string的成员函数、成员常量和非成员函数_秋秋晗晗的博客-CSDN博客中1.2 迭代器

1.3 容量

size    max_size    resize    capacity    empty    reserve    shrink_to_fit

与string的容器类似,详见【C++】string的成员函数、成员常量和非成员函数_秋秋晗晗的博客-CSDN博客中1.3 容器

1.4 元素访问

operator[]    at    front    back    data

与string的元素访问类似,详见【C++】string的成员函数、成员常量和非成员函数_秋秋晗晗的博客-CSDN博客中1.4 元素访问

1.4.1 遍历方法

与string的遍历方法类似,详见【C++】string的成员函数、成员常量和非成员函数_秋秋晗晗的博客-CSDN博客中1.4.1 遍历方法

1.5 修改器

assign    push_back    pop_back    insert    erase    swap    clear    emplace    emplace_back

与string的修改器类似,详见【C++】string的成员函数、成员常量和非成员函数_秋秋晗晗的博客-CSDN博客中1.5 修改器

1.6 配置器

get_allocatorallocator_type get_allocator() const;返回配置器

2. vector的非成员函数

relational operatorstemplate <class T, class Alloc>  bool operator== (const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs);
template <class T, class Alloc>  bool operator!= (const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs);
template <class T, class Alloc>  bool operator<  (const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs);
template <class T, class Alloc>  bool operator<= (const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs);
template <class T, class Alloc>  bool operator>  (const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs);
template <class T, class Alloc>  bool operator>= (const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs);
关系运算符重载
swaptemplate <class T, class Alloc>  void swap(vector<T, Alloc>& x, vector<T, Alloc>& y);交换vector的内容
http://www.lryc.cn/news/28907.html

相关文章:

  • RocketMQ安装
  • Spring——什么是IOC?
  • 力扣(LeetCode)430. 扁平化多级双向链表(2023.03.04)
  • 条款13:优先考虑const_iterator而非iterator
  • 23考研 长安大学846计算机考研复试《数据库》
  • Android 9.0 系统去掉省电模式
  • 3 mmmmm
  • nvidia Jetson nano Linux内核编译
  • 理想汽车2023年销量冲击30万辆有戏吗?
  • 借助CatGPT让turtlesim小乌龟画曲线
  • Java面试总结(四)
  • 强强联合,再强的英伟达NVIDIA也不落俗套
  • maven使用心得
  • 【算法题】1958. 检查操作是否合法
  • 十一、GoF之代理模式
  • MySQL5.6和JVM(1.6)调优
  • 【汇编】三、寄存器(一只 Assember 的成长史)
  • TFT通信协议解析与应用
  • python 操作word库docx 增强接口
  • ARM uboot 源码分析9 - uboot的硬件驱动部分
  • Mybatis动态sql语句foreach中拼接正则表达式字符串注意事项
  • JVM内置锁synchronized关键字详解
  • 【2021.12.25】xv6系统入门学习
  • Linux内核4.14版本——drm框架分析(4)——crtc分析
  • 用原生js手写分页功能
  • CornerNet介绍
  • 【SpringBoot】日志使用
  • 关于slice扩容性能损耗的探究
  • Java实现单向链表
  • 3月4日,30秒知全网,精选7个热点