ฉันกำลังพยายามหาดัชนีขององค์ประกอบในเวกเตอร์ของ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เป็นสตริงประเภท!