After using the find function to check if string check;
is in a vector<pair<string, string>>
by checking the pair[i].first
, i would like to get the elements in pair[i].second
into a separate string string holder;
how would I do this without using loops or is it inevitable and that's the only way? Can the find function return the value in addition to the true or false?
Dereference the iterator returned by the std::find function. The underlying value the iterator points to is of type std::pair. To access the pair's underlying member objects called first
and second
via an iterator use ->first
and ->second
respectively:
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';
}