当前位置:网站首页>41.【vector应用操作2】
41.【vector应用操作2】
2022-07-30 06:29:00 【李在奋斗……】
1.【单尾部插入】
函数名.push_back(a), 向尾部插入一个元素a,只能一个个插入,且只能在尾部。
2.【多元插入】 从哪插 插什么
函数名1.insert(函数名1.begin(),a), 向头部插入一个元素a.
函数名1.insert(函数名1.end(),n,a),向尾部插入n个元素 a.
3.【普通数组插入动态数组】
函数名1.insert(函数名1.begin(),普数组名,普数组名+sizeof(普数组名)/sizeof(函数类型))
从哪开始 普通数组首位置,普通数组末位置
4.【单尾部删除元素】
函数名.pop_back() 删除最后一个元素
5.【多元删除】
函数名.(函数名.begin(),函数名.begin()+n)
从哪开始,删除几个.(只能是begin 和end不能换成数字)
6.【for遍历】
for (int i = 0; i < 函数名.size(); i++) //遍历
{
cout << 函数名[i] << " ";
}
7.【迭代遍历】
vector<函数类型>::iterator itor;
for (itor = 函数名.begin(); itor != 函数名.end(); itor++)
{
cout << *itor << " ";
}
===================
#include <vector>
#include <iostream>
using namespace std;
int main()
{
int a[20] = {1,2,3,4,5};
vector<int> str_a; //初始化为空
vector<int> str_a1(4, 88); // 定义四个元素,每个元素的值为88;
vector<int> str_a4(a, a + sizeof(a)/sizeof(int)); //复制正常数组的初始化
int a5 = str_a4[2]; //vector 动态数组的访问,用下标
int b = str_a4.at(2); // 利用at函数,也就是下下标
cout << "a=" << a5 << " " << "b=" << b << endl;
cout << "str_a4的长度为:" << str_a4.size() << endl; //获取长度,
cout << "str_a4的第一个元素为:" << str_a4.front() << endl; // 获取第一个元素
cout << "str_a4的第一个元素为:" << str_a4.back() << endl; //获取最后一个元素
bool p = str_a4.empty(); //判断是否为空
str_a4.swap(str_a1);
str_a4.push_back(100); // 只能向尾部插入元素. 一个一个插入
//str_a.clear(); //对数组元素清空
str_a4.insert(str_a4.begin(), 888); // 从哪插入, 插入什么元素
str_a4.insert(str_a4.begin(),3,888); // 从哪插入, 插入几个 插入什么元素
int szint1[] = { 12,13,45 };
str_a4.insert(str_a4.end(), szint1, szint1 + sizeof(szint1) / sizeof(int));
//str_a4.pop_back(); //删除最后一个元素; 一个一个删除
//str_a4.erase(str_a4.begin(), str_a4.begin()+2); // 从哪开始, 删除几个
//str_a4.erase(str_a4.begin(), str_a4.end());
for (int i = 0; i < str_a4.size(); i++) //遍历
{
cout << str_a4[i] << " ";
}
vector<int>::iterator itor; //迭代器遍历
for (itor = str_a4.begin(); itor != str_a4.end(); itor++)
{
cout << *itor << " ";
}
return 0;
}


边栏推荐
- WinForm(一):开始一个WinForm程序
- 【BERT-多标签文本分类实战】之二——BERT的地位与名词术语解释
- IDEA搜索插件无结果一直转圈圈的解决办法
- Basic usage of tree arrays
- Go combines Gin to export Mysql data to Excel table
- Mybitatis related configuration files
- Electron使用romote报错 : Uncaught TypeError: Cannot read property ‘BrowserWindow‘ of undefined
- Go 使用 freecache 缓存
- BGP:边界网关路由协议 无类别的路径矢量EGP协议
- 物联网网关该怎么选
猜你喜欢

C# uses RestSharp to implement Get, Post requests (2)

【雷达目标检测】恒定阈值法和恒虚警(CFAR)法及代码实现

Basic usage of tree arrays

Go 使用 freecache 缓存

SOFA Weekly|Meetup 广州站、本周 QA、本周 Contributor

2020 ACM | MoFlow: An Invertible Flow Model for Generating Molecular Graphs

bean的生命周期

Mybitatis相关配置文件

mysql高阶语句(一)

DP5340国产替代CM5340立体声音频A/D转换器芯片
随机推荐
【防作弊】Unity防本地调时间作弊
RT-Thread-GD32的SPI在切换同一总线不同挂载设备时会出现切换后乱发送数据的问题
Vue项目通过node连接MySQL数据库并实现增删改查操作
redis常用指令
sizeof
IDEA搜索插件无结果一直转圈圈的解决办法
防止资源导出失败
SkiaSharp 之 WPF 自绘 拖曳小球(案例版)
this and super
树状数组的基本用法
LeetCode:204. 计数质数
IDEA search plug-in has no results and the solution has been spinning in circles
Distributed lock development
Keil软件中map文件解析
mysql设置会话超时时间
Go 结合Gin导出Mysql数据到Excel表格
go : 使用gorm修改数据
Burpsuite几种爆破方式
代币(双代币)系统研究
DNS domain name resolution services