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


边栏推荐
猜你喜欢
随机推荐
Monkey and Banana
ETL为什么经常变成ELT甚至LET?
golang : Zap日志整合
k8s 部署mysql8(PV和PVC 版本)
Mybitatis related configuration files
架构设计指南 如何成为架构师
分布式锁开发
适合程序员的输入法
Hex conversion...
ArrayList
Vue项目通过node连接MySQL数据库并实现增删改查操作
Map file analysis in Keil software
Go combines Gin to export Mysql data to Excel table
Keil软件中map文件解析
Limit injection record of mysql injection in No. 5 dark area shooting range
go : 使用gorm修改数据
Get all interface paths and names in the controller
DNS domain name resolution services
你好,我的新名字叫 “铜锁 / Tongsuo”
WinForm(一):开始一个WinForm程序









