news 2026/2/7 9:06:13

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/2/6 15:47:40

学长亲荐10个AI论文工具,本科生搞定毕业论文不求人!

学长亲荐10个AI论文工具&#xff0c;本科生搞定毕业论文不求人&#xff01; AI 工具如何让论文写作变得轻松高效&#xff1f; 在当今学术环境中&#xff0c;越来越多的本科生开始借助 AI 工具来辅助论文写作。这些工具不仅能显著降低 AIGC&#xff08;人工智能生成内容&#xf…

作者头像 李华
网站建设 2026/2/4 16:45:39

【课程设计/毕业设计】基于java教材征订管理系统基于微服务教材征订系统【附源码、数据库、万字文档】

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

作者头像 李华
网站建设 2026/2/6 7:36:55

2025国内评价高的宠物项圈权威榜单重磅发布

行业痛点分析当前宠物项圈领域面临着诸多技术挑战。一方面&#xff0c;定位精度不足是一大难题。在复杂城市环境中&#xff0c;宠物活动范围广&#xff0c;传统项圈的定位误差较大&#xff0c;难以准确追踪宠物位置。测试显示&#xff0c;部分项圈在高楼林立区域的定位误差可达…

作者头像 李华
网站建设 2026/2/4 3:43:51

为了过知网检测,我自费测了十大降AI平台,最好用的都在这了

家人们&#xff0c;现在学校查得是真严&#xff0c;不仅重复率&#xff0c;还得降ai率&#xff0c;学校规定必须得20%以下... 折腾了半个月&#xff0c;终于把市面上各类方法试了个遍&#xff0c;坑踩了不少&#xff0c;智商税也交了。今天这就把这份十大降AI工具合集掏心窝子…

作者头像 李华
网站建设 2026/2/7 0:56:44

【在Ubuntu22.04下,微星z790主板装机后无wifi模块】

在Ubuntu22.04下&#xff0c;微星z790主板装机后无wifi模块因为微星最新一批的支持wifi7的主板全都是高通的wifi模块&#xff0c;所以装机后没有网卡驱动&#xff0c;不显示wifi&#xff0c;解决方法为找一跟数据线把手机连接到电脑主机上&#xff0c;开启usb网络共享&#xff…

作者头像 李华