C++ std::vector<std::string> 遍历方法详解 - 使用for循环和迭代器
要遍历std::vectorstd::string,可以使用for循环或者迭代器。
例如,使用for循环遍历std::vectorstd::string可以这样写:
std::vector<std::string> myVector = {\"Hello\", \"World\", \"!\"};
for (int i = 0; i < myVector.size(); i++) {
std::cout << myVector[i] << std::endl;
}
另一种方法是使用迭代器来遍历std::vectorstd::string:
std::vector<std::string> myVector = {\"Hello\", \"World\", \"!\"};
for (std::vector<std::string>::iterator it = myVector.begin(); it != myVector.end(); ++it) {
std::cout << *it << std::endl;
}
无论使用哪种方法,都可以逐个访问std::vectorstd::string中的元素并进行操作。
原文地址: https://www.cveoy.top/t/topic/qaGi 著作权归作者所有。请勿转载和采集!