现在有一类型:std::vector<std::pair<std::string, std::vector<string>>>,请问如何分别取到std::pair中的std::string和std::vector<string>呢
时间: 2023-11-20 20:07:45 浏览: 105
利用C++实现从std::string类型到bool型的转换
要分别取到`std::pair`中的`std::string`和`std::vector<string>`,可以使用`std::get`函数。示例代码如下:
```cpp
#include <iostream>
#include <vector>
#include <string>
int main() {
std::vector<std::pair<std::string, std::vector<std::string>>> vec;
// 添加一些数据到vec中
vec.push_back(std::make_pair("pair1", std::vector<std::string>{"string1", "string2", "string3"}));
vec.push_back(std::make_pair("pair2", std::vector<std::string>{"string4", "string5"}));
vec.push_back(std::make_pair("pair3", std::vector<std::string>{"string6"}));
// 遍历vec,取出每个pair中的string和vector<string>
for (const auto& pair : vec) {
std::cout << "First string: " << std::get<0>(pair) << std::endl;
std::cout << "Second vector of string: ";
const auto& vectorOfString = std::get<1>(pair);
for (const auto& str : vectorOfString) {
std::cout << str << " ";
}
std::cout << std::endl;
}
return 0;
}
```
输出结果:
```
First string: pair1
Second vector of string: string1 string2 string3
First string: pair2
Second vector of string: string4 string5
First string: pair3
Second vector of string: string6
```
阅读全文