std::vector 是一种 STL 容器,它可以存储一组连续的、可增长的数据。可以使用迭代器来遍历 vector 中的元素。
迭代器是一种对象,它指向序列中的某个元素,并支持在序列中移动。
要在 vector 中遍历元素,可以这样做:
#include <vector>
#include <iostream>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
for (auto it = v.begin(); it != v.end(); ++it) {
std::cout << *it << std::endl;
}
return 0;
}