I am using C++ and i have 2 vectors that a related to each other:
vector<double> val = {.3,.5,.2,.4};
vector<string> str = {'a','b','c','d'};
I would like to search val for the max, and then return the string from str in the same position:
vector<double>::const_iterator it;
it = max_element(val.begin(), val.end());
So, how can i use it inside str to get the letter?
string lettter;
letter = str.at(it-> ????? );
Thank!!!
You can find out how far
itis from the beginning ofvaland then use that to indexstr:By using
std::distance, this will still work if you change the type ofvalto a container whose iterator does not provide random access. However, when using it on a random access iterator, you will still get constant time complexity. Usingstd::beginallows you to changevalto an C-style array if you ever wanted to.It’s worth mentioning that you should be initialising
strwith:std::stringhas no constructor that takes achar.