のベクトルの要素のインデックスを取得しようとしています。これstringsをintタイプの別のベクトルのインデックスとして使用しますが、これは可能ですか?
例:
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文字列型なので、明らかにこれは機能しません!
これはstackoverflow.com/questions/1425349/…
—
Francesco Vollero 2013
std :: mapまたはstd :: unordered_mapをチェックアウトする必要があります。
—
Etherealone 2013