如果我使用 std:find 在向量对中找到一个元素,我如何才能将向量中的值转换为字符串?

If I find an alement in a vector pairs using std:find, how can I get the values from the vector into a string?

在使用查找函数通过检查 pair[i].first 来检查 string check; 是否在 vector<pair<string, string>> 中之后,我想将 pair[i].second 中的元素放入一个单独的字符串 string holder;

如果不使用循环我将如何做到这一点,或者它是不可避免的并且这是唯一的方法吗? find函数可以return取值除真还是假?

std::find returns 指向已找到项的迭代器,如果未找到则为 end()。因此,如果找到它,您就有一个指向您的对的迭代器。

取消引用 std::find function. The underlying value the iterator points to is of type std::pair 返回的迭代器。要通过迭代器访问名为 firstsecond 的对的基础成员对象,分别使用 ->first->second

std::vector<std::pair<std::string, std::string>> v { {"aa", "bb"}, { "cc", "dd" }};
auto mypair = std::make_pair<std::string, std::string>("cc", "dd");
auto foundit = std::find(std::begin(v), std::end(v), mypair);
std::string s1;
std::string s2;
if (foundit != std::end(v)) {
    s1 = foundit->first; // first element
    s2 = foundit->second; // second element
}
else {
    std::cout << "v does not contain the pair." << '\n';
}