Friday, 15 August 2014

C++ search vector for MAX, and get same position form a second vector -



C++ search vector for MAX, and get same position form a second vector -

i using c++ , have 2 vectors related each other:

vector<double> val = {.3,.5,.2,.4}; vector<string> str = {'a','b','c','d'};

i search val max, , homecoming string str in same position:

vector<double>::const_iterator it; = max_element(val.begin(), val.end());

so, how can utilize it within str letter?

string lettter; letter = str.at(it-> ????? );

thank!!!

you can find out how far it origin of val , utilize index str:

str[std::distance(std::begin(val), it)]

by using std::distance, still work if alter type of val container iterator not provide random access. however, when using on random access iterator, still constant time complexity. using std::begin allows alter val c-style array if ever wanted to.

it's worth mentioning should initialising str with:

vector<string> str = {"a","b","c","d"};

std::string has no constructor takes char.

c++ vector

No comments:

Post a Comment