写这篇文章主要是为了记录vector的一些用法,之前一直没有过系统的记录,导致自己老是忘记
遍历
1、下标遍历
cpp
#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、迭代器遍历
cpp
//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、反向遍历
cpp
// C++11+
for (auto it = a.rbegin(); it != a.rend(); it++) {
cout << *it << " ";
}
4、特定位置开始遍历
cpp
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、遍历矩阵
cpp
#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、初始化
cpp
// 初始化时赋值多个元素
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、拷贝
cpp
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
cpp
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(修改特定区间的值为特定)
cpp
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 的区间是左闭右开
某个特定元素出现的个数
cpp
#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;
}
求和
cpp
#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
cpp
#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;
}
求最大值最小值
cpp
#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;
}