news 2026/7/16 21:28:03

STL——vector

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
STL——vector

写这篇文章主要是为了记录vector的一些用法,之前一直没有过系统的记录,导致自己老是忘记

遍历

1、下标遍历

#include <bits/stdc++.h> using namespace std; int main() { vector<int> a = {1, 2, 3, 4, 5}; // 下标遍历 for (int i = 0; i < a.size(); i++) { cout << a[i] << " "; } return 0; } // 输出:1 2 3 4 5

2、迭代器遍历

//c++ 11以上 vector<int> a = {1, 2, 3, 4, 5}; // 范围for遍历(只读) for (auto num : a) { cout << num << " "; } // 如需修改元素,用引用 for (auto &num : a) { num *= 2; // 所有元素乘2,a变为{2,4,6,8,10} }

3、反向遍历

// C++11+ for (auto it = a.rbegin(); it != a.rend(); it++) { cout << *it << " "; }

4、特定位置开始遍历

vector<int> a = {1, 2, 3, 4, 5}; int start = 2; // 从下标2(第三个元素)开始遍历 for (int i = start; i < a.size(); i++) { cout << a[i] << " "; // 输出:3 4 5 } // 场景1:从下标2开始,遍历到末尾 for (auto it = a.begin() + 2; it != a.end(); it++) { cout << *it << " "; // 输出:3 4 5 } // 场景2:遍历下标1到3(左闭右开,即1、2、3) for (auto it = a.begin() + 1; it != a.begin() + 4; it++) { cout << *it << " "; // 输出:2 3 4 } // 场景3:反向遍历最后3个元素 for (auto it = a.rbegin(); it != a.rbegin() + 3; it++) { cout << *it << " "; // 输出:5 4 3 }

5、遍历矩阵

#include <bits/stdc++.h> using namespace std; int main() { vector<vector<int>> mat = {{1, 2}, {3, 4}, {5, 6}}; // 外层:遍历每一行(row是当前行的vector<int>) for (auto& row : mat) { // 用引用&避免拷贝,提升效率 // 内层:遍历当前行的每个元素 for (auto num : row) { cout << num << " "; } cout << endl; } return 0; }

赋值

1、初始化

// 初始化时赋值多个元素 vector<int> a = {1, 2, 3, 4, 5}; // C++11支持 vector<string> b{"apple", "banana", "orange"}; // 初始化长度为5,所有元素为0 vector<int> a(5, 0); // 初始化长度为3,所有元素为9 vector<int> b(3, 9);

2、拷贝

vector<int> a = {1, 2, 3}; vector<int> b; // 将a的所有值拷贝给b,b变为{1,2,3} b = a; // 覆盖赋值:b原有值被替换为{4,5} b = {4, 5};

3、assign

vector<int> a; // 先赋值5个8,a变为{8,8,8,8,8} a.assign(5, 8); // 覆盖原有值,赋值3个6,a变为{6,6,6} a.assign(3, 6); // 输出:6 6 6 vector<int> a = {1, 2, 3, 4, 5}; vector<int> b; // 赋值a的全部区间(begin()到end()),b变为{1,2,3,4,5} b.assign(a.begin(), a.end()); vector<int> c; // 赋值a的下标1到3的区间(左闭右开),c变为{2,3,4} c.assign(a.begin() + 1, a.begin() + 4); // 数组转vector int arr[] = {10, 20, 30}; vector<int> d; // 赋值数组的全部元素,d变为{10,20,30} d.assign(arr, arr + sizeof(arr)/sizeof(int));

4、fill(修改特定区间的值为特定)

vector<int> a = {1,2,3,4,5}; int k = 2; fill(a.end() - k, a.end(), 0); // 最后2个元素改为0 // 输出:1 2 3 0 0 vector<int> a = {1,2,3,4,5}; int k = 3; fill(a.begin(), a.begin() + k, 8); // 前3个元素改为8 // 输出:8 8 8 4 5 vector<int> a = {1,2,3,4,5}; // 修改倒数第3个到倒数第1个元素为7 fill(a.rbegin() + 1, a.rbegin() + 4, 7); // 输出:1 7 7 7 5

注意:fill的区间是左闭右开

某个特定元素出现的个数

#include <bits/stdc++.h> using namespace std; int main() { vector<int> a = {1, 2, 3, 2, 2, 4, 5, 2}; int target = 2; // 要统计的目标元素 // 统计整个vector中target的个数 int cnt = count(a.begin(), a.end(), target); cout << "元素 " << target << " 出现的次数:" << cnt << endl; // 输出:4 return 0; }

求和

#include <bits/stdc++.h> // 注意:accumulate属于<numeric>头文件,bits/stdc++.h已包含 using namespace std; int main() { vector<int> a = {1, 2, 3, 4, 5}; // 核心语法:accumulate(起始迭代器, 结束迭代器, 初始值) int sum = accumulate(a.begin(), a.end(), 0); // 初始值0(和元素类型一致) // 浮点型示例 vector<double> b = {1.5, 2.5, 3.0}; double sum_d = accumulate(b.begin(), b.end(), 0.0); // 初始值用0.0,避免精度丢失 cout << "int总和:" << sum << endl; // 输出:15 cout << "double总和:" << sum_d << endl; // 输出:7.0 return 0; } // (自定义)累加元素的平方和:1²+2²+3²+4²+5²=55 int sum_sq = accumulate(a.begin(), a.end(), 0, [](int total, int num) { return total + num * num; }); // 指定区间求和 vector<int> a = {1, 2, 3, 4, 5}; // 求和下标1到3(左闭右开)的元素:2+3+4=9 int sum = accumulate(a.begin() + 1, a.begin() + 4, 0); cout << "区间总和:" << sum << endl; // 输出:9

查找元素find

#include <bits/stdc++.h> using namespace std; int main() { vector<int> a = {10, 20, 30, 20, 40}; int target = 20; // 核心语法:find(起始迭代器, 结束迭代器, 目标元素) auto it = find(a.begin(), a.end(), target); // 判断是否找到:迭代器不等于end()即为找到 if (it != a.end()) { // 迭代器转下标:it - a.begin() cout << "元素 " << target << " 存在,第一个位置:" << it - a.begin() << endl; // 输出:1 } else { cout << "元素 " << target << " 不存在" << endl; } return 0; }

求最大值最小值

#include <bits/stdc++.h> using namespace std; int main() { vector<int> nums = {3, 1, 4, 1, 5, 9, 2, 6}; if (nums.empty()) { cout << "vector为空" << endl; return 0; } // 核心语法:min_element(起始迭代器, 结束迭代器) auto min_it = min_element(nums.begin(), nums.end()); auto max_it = max_element(nums.begin(), nums.end()); // 迭代器解引用获取值 int min_val = *min_it; int max_val = *max_it; cout << "最小值:" << min_val << endl; // 输出:1 cout << "最大值:" << max_val << endl; // 输出:9 return 0; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/16 9:57:48

不用懂代码也能护网站!雷池雷池 SafeLine 的硬核防护指南

文章目录前言【视频教程】1.安装Docker2.本地部署SafeLine3.使用SafeLine4.cpolar内网穿透工具安装5.创建远程连接公网地址6.固定Uptime Kuma公网地址前言 雷池 SafeLine 是一款专为中小企业和开发者打造的开源 WAF&#xff08;Web 应用防火墙&#xff09;&#xff0c;能自动拦…

作者头像 李华
网站建设 2026/7/16 5:46:45

spring-事务

一.事务简介&#xff08;以银行账户转账为例&#xff09; 1.案例简述 在本案例中&#xff0c;我们使用了 Spring 事务管理来确保银行账户的转账操作在出现问题时能够正确回滚&#xff0c;保持数据的一致性。&#xff08;转账操作要么双方都成功&#xff0c;要么都失败&#xf…

作者头像 李华
网站建设 2026/7/16 9:27:17

2026科技热点预言:CES Asia“具身智能”展区已成产业风向标

2025年12月25日消息&#xff0c;当具身智能从技术概念迈入量产爆发前夜&#xff0c;CES Asia 2026“具身智能”展区的招商数据已提前锁定2026年产业风向。基于全球头部企业入驻名单的深度技术解析显示&#xff0c;人形机器人、AI大模型融合、高精度灵巧操作三大细分赛道已形成集…

作者头像 李华
网站建设 2026/7/16 13:05:40

MySQL 执行计划 EXPLAIN 常见的 Extra 信息解析

在 MySQL 的 EXPLAIN 输出中&#xff0c;Extra 列提供了关于查询执行计划的额外信息&#xff0c;这些信息有助于理解查询是如何被优化的、使用了哪些策略以及是否存在潜在的性能问题。下面是一些常见的 Extra 信息及其解释&#xff1a;常见 Extra 信息解析 1. Using index 表示…

作者头像 李华
网站建设 2026/7/16 7:25:33

一文看懂AI大模型的核心模块:基于强化学习的偏好对齐原理及其应用

无论是语言模型还是推荐大模型&#xff0c;基于强化学习的偏好对齐已成为其训练流程中不可或缺的关键环节。 本文将系统梳理强化学习在大模型中的实践路径&#xff1a;首先阐明其核心机制与主流算法&#xff0c;继而聚焦语言模型与推荐大模型的典型应用场景&#xff0c;深入解…

作者头像 李华