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


边栏推荐
猜你喜欢

2020 数学建模之旅

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

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

IDEA 中CheckStyle安装及使用

Interview with Ant: How do these technology pioneers do the bottom-level development well?| Excellent technical team interview

Go uses freecache for caching

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

How to calculate the daily cumulative capital flow one by one in real time

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

C language custom types, rounding
随机推荐
[GO语言基础] 一.为什么我要学习Golang以及GO语言入门普及
redis常用指令
this与super
go : use gorm to modify data
【Codeforces Round #805 (Div. 3)(A~C)】
防止资源导出失败
解决datagrip连接sqlserver报错:[08S01] 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。
Electron日常学习笔记
SOFA Weekly|Meetup 广州站、本周 QA、本周 Contributor
Basic usage of tree arrays
Vue2进阶篇-编程式路由导航、缓存路由组件、路由的激活与失活
go : go gin返回JSON数据
孙洪鹤讲教材:原点+众筹+产品,逆向营销实战操作方案
MYSQL下载及安装完整教程
mysql8的my.conf配置文件参考指引
interface
assert
LeetCode:204. 计数质数
selenium模块
K-Net:Towards Unified Image Segmentation,基于动态内核的通用分割网络,(NMS-free and Box-free),从语义/实例分割到全景分割。