在Vector中更新值

时间:2010-11-22 02:22:25

标签: c++

   void Record::Update() {

string choice;

cout << "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;
     }

   }
 }

此功能通过其唯一ID号查找记录。每个新记录都有一个ID号。如果我输入ID 2,该功能将显示ID为2的记录。如何修改记录的某个属性?在这种情况下它的位置。

2 个答案:

答案 0 :(得分:0)

您将Transit对象编程为setXXX()方法,然后调用该方法。迭代器的工作方式大多类似于指针,因此您可以使用setter方法更改类变量。

答案 1 :(得分:0)

    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;
        }

  }
}