Partial Updating of Struct in a Vector

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jwwicks
    New Member
    • Oct 2007
    • 19

    Partial Updating of Struct in a Vector

    Hello Experts,

    This is a student question and I know you could use a list or some other keyed container but they haven't been introduced to the class so they aren't free game so to speak.

    So...Given a struct And a vector of those structs is their a way to partially update just first_name and last_name. I can find an account using a simple bool function and find_if but trying to use that iterator causes an error.

    Code:
    struct customer{
    	string account;
    	string first_name;
    	string last_name;
    	string address;
    	string city;
    	string state;
    	string zip;
    	string phone;
    	double acct_balance;
    	Date* date;
    };
    
    vector<customer> customers;
    string t, id;
    typedef vector<customer>::const_iterator VCI;
    
    // Call a function to get account id from user
    // This works fine
    
    // Find the customer in the vector with the account id
    // This works fine
    t = id;
    transform( t.begin(), t.end(), t.begin(), toupper );
    VCI f = find_if( customers.begin(), customers.end(), cust_eq(t));
    
    // Create a little temporary to get new information
    customer* uCust = new( customer );
    customer& cUpdate = *uCust;
    
    // Call some function to just get the first_name and last_name into cUpdate
    // This works fine...
    do_cust_name( cUpdate );
    
    //but use the following code
    (*f).first_name = cUpdate.first_name;
    
    // produces main.cpp(249): error C2678:
    //binary '=' : no operator found which takes a left-hand
    //operand of type 'const std::string' (or there is no acceptable conversion)
    Why is this const now?? Should I, Can I, cast const away in this context??

    I can't use a wholesale copy because only part of the members of the struct are actually updated...

    Can't use customer.at() since I don't have an index...

    Some hints please...

    DUH! answered my own question just looked at the code above I used a const_iterator should have used iterator instead. That fixed it.

    Is there a better way to do this ??

    JWEMAIL REMOVED BY MODERATOR.
  • RRick
    Recognized Expert Contributor
    • Feb 2007
    • 463

    #2
    You've got the right idea. Iterators will allow you to modify the internal values to the struct in the vector. Const iterators won't allow you to change the values.

    There is a simple nit-pick where you don't need to new and set a reference before passing it to do_cust_name. Your temporary can be a local variable and you'll get the same results.
    [code=cpp]customer cUpdate;
    do_cust_name( cUpdate );[/code]

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by jwwwicks
      (*f).first_name = cUpdate.first_n ame;

      // produces main.cpp(249): error C2678:
      //binary '=' : no operator found which takes a left-hand
      //operand of type 'const std::string' (or there is no acceptable conversion)
      You did not include <string>.

      Comment

      • jwwicks
        New Member
        • Oct 2007
        • 19

        #4
        Originally posted by RRick
        You've got the right idea. Iterators will allow you to modify the internal values to the struct in the vector. Const iterators won't allow you to change the values.

        There is a simple nit-pick where you don't need to new and set a reference before passing it to do_cust_name. Your temporary can be a local variable and you'll get the same results.
        [code=cpp]customer cUpdate;
        do_cust_name( cUpdate );[/code]
        Thanks for the feedback...

        John

        Comment

        • jwwicks
          New Member
          • Oct 2007
          • 19

          #5
          Originally posted by weaknessforcats
          You did not include <string>.
          Umm, not quite. I just didn't post the whole main etc.. trying to be brief to spare forum members. The #include <string> is there, it was just that the typedef vector<customer >::const_iterat or should have been just typedef vector<customer >::iterator.

          Thanks for the feedback though...

          John

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Oops. I missed the part about const std::string in the error message.

            I'm going for more coffee now.

            Sorry.

            Comment

            • jwwicks
              New Member
              • Oct 2007
              • 19

              #7
              Just a follow on to ask about style...

              I have the following function and it works fine updating the information in the vector of structs. The question I have is should I change the cUpdate reference from *uCust to *f and just update the element in the vector directly without the use of a temp. The do_cust_xxxx functions all take a customer& as an argument. I'm toying with the idea since I don't see anything in C++ that say I can't do customer cUpdate& = *f; That would eliminate all the (*f).this_value = cUpdate.this_va lue; stuff right?

              Code:
              /////////////////////////
              // \fn do_update( vector<customer>& c )
              // \brief Handling routine for update menu
              //
              // \b Purpose: Processes update menu choices
              // \b SideEffects: none
              //
              // \param c - vector containing customer records
              // \return none
              /////////////////////////
              void do_update( vector<customer>& c )
              {
              	bool done = false;
              	int choice;
              	string t, id;
              
              	customer* uCust = new( customer );
              	customer& cUpdate = *uCust;
              	
              	do{
              		system(CLRSCR);
              		show_copyright();
              		do
              		{
              			id = get_customer_id();
              			cout << endl << "Modifying Customer : " << id << endl << endl;
              		}while(!is_valid_customer_id( c, id ));
              
              		typedef vector<customer>::iterator VCI;
              		t = id;
              		transform( t.begin(), t.end(), t.begin(), toupper );
              		VCI f = find_if( c.begin(), c.end(), cust_eq(t));
              		
              		show_update_menu();
              		do{ 
              			choice = get_choice(); 
              		}while(!is_valid_update_choice( choice )); 
              		
              		switch( choice ){
              			case 1: do_cust_name( cUpdate );
              					(*f).first_name = cUpdate.first_name;
              					(*f).last_name = cUpdate.last_name;
              					done = true;
              					break;
              			case 2: do_cust_address( cUpdate );
              					do_cust_city( cUpdate );
              					do_cust_state( cUpdate );
              					do_cust_zip( cUpdate );
              					(*f).address = cUpdate.address;
              					(*f).city = cUpdate.city;
              					(*f).state = cUpdate.state;
              					(*f).zip = cUpdate.zip;
              					done = true;
              					break;
              			case 3: do_cust_phone( cUpdate );
              					(*f).phone = cUpdate.phone;
              					done = true;
              					break;
              			case 4: do_cust_balance( cUpdate );
              					(*f).acct_balance = cUpdate.acct_balance;
              					done = true;
              					break;
              			case 5: do_cust_payment( cUpdate );
              					(*f).date = cUpdate.date;
              					done = true;
              					break;
              			default:
              				done = true;
              				break;
              		}
              
              	}while(!done);
              
              	delete uCust;
              }
              John
              Last edited by jwwicks; Oct 6 '07, 07:35 PM. Reason: typo

              Comment

              Working...