Update Value in Vector
void Record::Update() {
string choice;
cout &l开发者_运维技巧t;< "Enter ID: " << endl;
cin >> IDValue;
for(Itr = List.begin() ; Itr !=List.end() ; Itr+) {
if(Itr->GetID() == IDValue)
{
cout << Transit->GetID() << endl;
cout << "Would you like to set Name ? (y/n) :";
cin >> choice;
if ( choice == 'y' )
cin >> strName;
Itr->SetName(strName);
cout << Itr->GetName() << endl;
cout << Itr->GetLocation() << endl;
}
}
}
This function finds a record by its unique ID number. Each new record is given an ID number. If I enter in ID 2 the function displays the record with ID 2. How can I modify one of the attributes of the record? In this case the location of it.
void RecordList::UpdateLocation() {
int IDValue;
char* strName;
char opt;
cout << "Enter ID number to update: " << endl;
cin >> IDValue;
for(Transit = List.begin() ; Transit !=List.end() ; Transit++) {
if(Transit->GetID() == IDValue)
{
cout << Transit->GetID() << endl;
cout << "Would you like to set Name ? (y/n) :";
cin >> opt;
if ( opt == 'y' )
cin >> strName;
Transit->SetName(strName);
cout << Transit->GetName() << endl;
cout << Transit->GetLocation() << endl;
}
}
}
You program the Transit object to have a setXXX()
method and then call that method. Iterators work mostly like pointers so you can change a class variable with a setter method.
精彩评论