C++ 遍历 stdvectorstring
可以使用迭代器或范围 for 循环来遍历 std::vector
使用迭代器:
#include <iostream>
#include <vector>
#include <string>
int main() {
std::vector<std::string> v = {"hello", "world", "how", "are", "you"};
for (auto it = v.begin(); it != v.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
return 0;
}
使用范围 for 循环:
#include <iostream>
#include <vector>
#include <string>
int main() {
std::vector<std::string> v = {"hello", "world", "how", "are", "you"};
for (const auto& s : v) {
std::cout << s << " ";
}
std::cout << std::endl;
return 0;
}
两种方法都可以遍历 std::vector
原文地址: https://www.cveoy.top/t/topic/g9aO 著作权归作者所有。请勿转载和采集!