我想要得到一個向量strings
中的一個元素的索引,將它用作另一個向量int
類型的索引,這可能嗎?如何獲取字符串向量中某個元素的位置,將其用作ints向量中的索引?
例子:
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);
}
現在我想要得到的old_name
位置在Names
載體,在Numbers
向量訪問某些元素來使用它。所以,我可以說:
Numbers[position] = 3 ; // or whatever value assigned here.
我試着使用:
vector <string> :: const_iterator pos;
pos = (find(Names.begin(), Names.end(), old_name_))
Numbers[pos] = 3;
但顯然,這並不工作,因爲pos
是字符串類型!
我想這應該http://stackoverflow.com/questions/1425349/how-do- i-find-an-element-position-in-stdvector – 2013-02-26 21:52:37
你應該檢查出std :: map或std :: unordered_map。 – Etherealone 2013-02-26 21:56:43