当前位置:网站首页>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;
}


边栏推荐
猜你喜欢

02 多线程与高并发 - synchronized 解析

物联网网关该怎么选

从 Google 离职,前Go 语言负责人跳槽小公司

DP5340 domestic replacement for CM5340 stereo audio A/D converter chip

Go combines Gin to export Mysql data to Excel table

“AI教练”请进家,家庭智能健身蓬勃发展

专访蚂蚁:这群技术排头兵,如何做好底层开发这件事?| 卓越技术团队访谈录

Keil软件中map文件解析

Limit injection record of mysql injection in No. 5 dark area shooting range

【MySQL】MySQL中如何实现分页操作
随机推荐
Go 使用mencached缓存
[硬核干货]由0到1,突破信息系统项目管理师(呕心沥血经验之谈)!!!
Interview with Ant: How do these technology pioneers do the bottom-level development well?| Excellent technical team interview
获取controller中所有接口路径和名称
C语言自定义类型详解
Max Sum Plus Plus HDU - 1024
goto语句
linux安装mysql8参考指引
go : use gorm to modify data
Electron使用romote报错 : Uncaught TypeError: Cannot read property ‘BrowserWindow‘ of undefined
golang: Gorm configures Mysql multiple data sources
MySQL off-topic [ORM thought analysis]
Limit injection record of mysql injection in No. 5 dark area shooting range
分布式锁开发
“AI教练”请进家,家庭智能健身蓬勃发展
ArrayList
Derivative Operations on Vectors and Derivative Operations on Vector Cross and Dot Products
ARM体系结构概述
理解和熟悉递归中的尝试
深度学习:线性回归模型