博客
关于我
stl序列式容器——vector
阅读量:552 次
发布时间:2019-03-09

本文共 1508 字,大约阅读时间需要 5 分钟。

stl序列式容器——vector

简单介绍

1.定义在头文件 <vector>

2.是最简单的序列式容器,可以随机访问元素
3.像一个动态数组
4.迭代器(类似指针)是随机存取迭代器,对任何一个STL算法均奏效,尤其在末端或删除元素时,性能很好。
5.可以实现数据结构中的队列deque、数组array、堆栈stack的所有功能。
6.一旦定义了类对象后,就可“装”东西了。

1.vector类基础

1.1定义

vector
myvt;

1.2初始化

使用函数push_back()

myvt.push_back("something");

1.3容器的大小和容量

size()测现有的元素数量

capacity()返回容器中实际能够容纳的元素数量
max_size()所能容纳最大元素数量
reserve()预先设置容器大小
resize()修改容器大小

2.类中的成员函数

2.1判断向量是否为空

empty()

clear() 将元素清空

while(!myvt.empty())

2.2遍历vector容器

  1. 迭代器方法实现遍历
  2. 使用at()函数和循环语句实现
sturct ST{   int id;double db;}void Iter_for(vector
& vt){ ST temp; vector
::iterator iter; for(iter=vt.begin();iter!=vt.end();iter++) { temp=*iter; cout<<"id: "<
<<", db: "<
<
& vt){ ST temp; int i=0; int m=myvt.size(); for(i=0;i

2.3使用算法

添加头文件<algorithm>

for_each算法进行输出

void out(Student& stu){   cout<<"id: "<
<<", score:"<
<

3.高级编程

3.1元素的访问

at() ,[], front(), back()

vector
c;c.at(index); //返回值是引用c[index]; //返回值是引用c.front(); //返回第一个元素c.back(); //返回最后一个元素

3.2迭代器相关函数

begin()

end()
rbegin()
rend()

3.3元素查找和搜索

find()

find if()

location_index=find(myvt.begin(),myvt.end(),2);//数字2的下标是location_index=find_if(myvt.begin(),myvt.end(),bind2nd(greater
(),5);//第一个大于5的数字下标是

3.4容器中字符串处理

3.5元素排序

vector容器中的元素排序需要使用算法sort()或者其他算法。

3.6插入元素

push_back()将元素插入末尾

insert()元素插入任意位置,返回值是迭代器,指向刚插入的元素。

3.7删除元素

pop_back()

erase()
clear()

3.8 对象交换

swap()

3.9 vector<bool>

转载地址:http://ttzsz.baihongyu.com/

你可能感兴趣的文章
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>