jeudi 29 juillet 2021

get second element of a vector pair from specific first element

how can i get the second element of a vector pair from specific first element?

I have codes below:

vector< pair <string, string> > vect;
vect.push_back(make_pair("hello","s:hihi"));
vect.push_back(make_pair("hi","s:hello"));
string check;
string first;
string last;
while(true)
{
    cout << "Create new pair.\nFirst element: ";
    //Part1
    getline(cin, first);
    cout << "Second element: ";
    //Part2
    getline(cin, last);
    if(first == "get" && last == "get") break;
    else vect.push_back(make_pair(first, last));
}
cout << "What first element to search: ";
//Part3
getline(cin, check);

I want to check if user entered hello at part3, it will cout s:hihi. By this way if user entered hi at part3, it will cout s:hello. If user entered "bye" at part 1 and entered "hey" at part 2. Then when user enter "bye" at part 3 it will print out "hey"

How can I do this?

Aucun commentaire:

Enregistrer un commentaire