当前位置:网站首页>40.【vector的运用】
40.【vector的运用】
2022-07-30 06:29:00 【李在奋斗……】
1.【动态数组的访问下标】
函数名[n], n就是访问第几个元素
2.【动态数组的访问at函数】
函数名.at(n), n就是访问第几个元素
3.【动态数组的长度】
函数名.size(), 获取长度
4.【获取动态数组第一个元素】
函数名.front()
5.【获取动态数组最后一个元素】
函数名.back()
6.【判断动态数组是否为空】
函数名.empty() ,假如是返回true.
7.【动态数组的交换】
函数名1.swap(函数名2) 把俩函数的内容交换
8.【动态数组元素的清空】
函数名.clear()
====================
#include <vector>
#include <iostream>
using namespace std;
int main()
{
int a[5] = {1,2,3,4,5};
vector<int> str_a; //初始化为空
vector<int> str_a1(4, 88); // 定义四个元素,每个元素的值为88;
vector<int> str_a2 = str_a1; //把a1的值复制给a2;
vector<int> str_a3(str_a1.begin(), str_a1.end()); //把a1的值复制给a2;
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.clear(); //对数组元素清空
return 0;
}


边栏推荐
猜你喜欢

What are the access modifiers, declaration modifiers, and keywords in C#?Literacy articles

Handler消息机制-Native层

C language custom types, rounding

2022牛客暑期多校训练营3(ACFGJ)

Burpsuite几种爆破方式

Go uses the mencached cache

K-Net:Towards Unified Image Segmentation,基于动态内核的通用分割网络,(NMS-free and Box-free),从语义/实例分割到全景分割。

入选“十大硬核科技”,详解可信密态计算(TECC)技术点

Go 使用mencached缓存

你好,我的新名字叫 “铜锁 / Tongsuo”
随机推荐
服务器可靠性稳定性调优指引
【雷达目标检测】恒定阈值法和恒虚警(CFAR)法及代码实现
Electron中设置菜单(Menu),主进程向渲染进程共享数据
Vue项目通过node连接MySQL数据库并实现增删改查操作
使用navicat连接mysql数据库时常报的错误:2003、1698、1251
SE11 创建搜索帮助
学生成绩管理系统(C语言)
【COCI 2020/2021 Round #2 D】Magneti(DP)
Monkey and Banana
代币(双代币)系统研究
sizeof
一段神奇的没有主方法的代码
LeetCode:647. 回文子串
防止资源导出失败
MySQL off-topic [ORM thought analysis]
docker部署redis一主二从三哨兵模式
Interview with Ant: How do these technology pioneers do the bottom-level development well?| Excellent technical team interview
A magical no main method of code
mysql8的my.conf配置文件参考指引
Go combines Gin to export Mysql data to Excel table