遍历std::vector<std::vector<std::string>> indexArr
时间: 2024-06-20 21:02:02 浏览: 13
遍历一个 `std::vector<std::vector<std::string>>` 结构,即二维字符串向量,通常涉及到两层循环,因为你需要遍历每个子向量中的字符串。这里是一个简单的示例,说明如何遍历:
```cpp
std::vector<std::vector<std::string>> indexArr; // 假设这是你要遍历的二维向量
// 遍历二维向量
for (size_t i = 0; i < indexArr.size(); ++i) { // 外层循环,遍历行
std::vector<std::string> row = indexArr[i]; // 获取当前行
for (size_t j = 0; j < row.size(); ++j) { // 内层循环,遍历列
std::string str = row[j]; // 获取当前元素
// 这里你可以对str执行任何操作,例如打印、处理或存储
std::cout << "Element at (" << i << ", " << j << "): " << str << std::endl;
}
}
```
相关问题
如何遍历std::vector<std::string>
遍历std::vector<std::string>的方法有很多种,这里给出两种常见的方法:
方法一:使用下标遍历
```cpp
#include <iostream>
#include <vector>
#include <string>
int main() {
std::vector<std::string> vec = {"hello", "world", "example"};
for (size_t i = 0; i < vec.size(); ++i) {
std::cout << vec[i] << std::endl;
}
return 0;
}
```
方法二:使用迭代器遍历
```cpp
#include <iostream>
#include <vector>
#include <string>
int main() {
std::vector<std::string> vec = {"hello", "world", "example"};
for (std::vector<std::string>::iterator it = vec.begin(); it != vec.end(); ++it) {
std::cout << *it << std::endl;
}
return 0;
}
```
std::vector<std::string>::iterator
std::vector<std::string>::iterator是一个用于遍历std::vector<std::string>容器的迭代器类型。在引用的代码中,theIterator是一个std::vector<std::string>容器的迭代器,用于循环遍历容器中的元素。在main函数中,通过将theIterator初始化为vtTemp.begin(),并在循环条件中判断theIterator是否等于vtTemp.end(),来遍历vtTemp容器中的所有元素。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>