news 2026/4/18 15:21:58

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/4/18 10:43:09

基于GA-BP的电涡流传感器称重系统温度补偿附matlab代码

✅作者简介&#xff1a;热爱科研的Matlab仿真开发者&#xff0c;擅长数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。&#x1f34e; 往期回顾关注个人主页&#xff1a;Matlab科研工作室&#x1f34a;个人信条&#xff1a;格物致知,完整Matlab代码获取及仿真…

作者头像 李华
网站建设 2026/4/18 12:29:14

Java计算机毕设之基于SpringBoot+微服务教材征订系统基于SpringBoot的高校教材征订管理系统(完整前后端代码+说明文档+LW,调试定制等)

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华
网站建设 2026/4/18 2:04:04

回溯+位运算|前缀和优化背包

汉字写一遍代码思路 可以提升和人沟通 和加深理解所以不可以只写完代码就去去丸了 还是要复习lc756class Solution { public:bool pyramidTransition(string bottom, vector<string>& allowed) {vector<int> groups[7][7];for (auto& s : allowed) {// A~F…

作者头像 李华
网站建设 2026/4/18 2:02:26

PostgreSQL 时间/日期处理指南

PostgreSQL 时间/日期处理指南 引言 PostgreSQL 是一款功能强大的开源关系型数据库系统,它提供了丰富的数据类型和功能,其中包括对时间/日期数据的支持。本文将详细介绍 PostgreSQL 中时间/日期类型的使用方法,包括数据类型、常用函数、操作和注意事项。 PostgreSQL 时间…

作者头像 李华
网站建设 2026/4/18 2:04:46

Doris资源组管理:精细化资源分配策略

Doris资源组管理:精细化资源分配的"食堂排队秘诀" 关键词:Doris资源组、精细化资源分配、查询优化、资源隔离、队列调度、Cgroup、多租户管理 摘要:当Doris作为大规模数据查询的"餐厅"时,如何让"食客"(查询)快速吃到"饭"(结果)…

作者头像 李华