C++中可以使用循环来遍历vector数组。以下是两种常见的遍历方式:
使用for循环遍历vector数组:#include <iostream>#include <vector>int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; // 使用for循环遍历vector数组 for (int i = 0; i < vec.size(); i++) { std::cout << vec[i] << " "; } return 0;}使用迭代器遍历vector数组:#include <iostream>#include <vector>int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; // 使用迭代器遍历vector数组 for (std::vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) { std::cout << *it << " "; } return 0;}无论是使用for循环还是使用迭代器,都可以依次访问vector数组中的每个元素。

