I am trying to get the index of an element in a vector of strings
, to use it as an index in another vector of int
type, is this possible ?
Example:
vector <string> Names;
vector <int> Numbers;
...
// condition to check whether the name exists or not
if((find(Names.begin(), Names.end(), old_name_)) != Names.end())
{ // if yes
cout <<"Enter the new name."<< endl;
cin >> name;
replace(Names.begin(), Names.end(), old_name_, name);
}
Now I want to get the position of old_name
in the Names
vector, to use it in accessing certain element in Numbers
vector. So that I can say:
Numbers[position] = 3 ; // or whatever value assigned here.
I tried using:
vector <string> :: const_iterator pos;
pos = (find(Names.begin(), Names.end(), old_name_))
Numbers[pos] = 3;
but obviously this doesn't work since pos
is of type string !
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…