Thread: Add multiple data in a std::set using +=operator

  1. #1
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194

    Add multiple data in a std::set using +=operator

    hi

    can anyone tel me how to insert multiple data at once in a std::set using an +=operator?

    i've created this function to insert a product into a set, but i don't know how to use an +=operator :-(

    Code:
    void grocery::insertProducts()
    {
    	set <product> Prod;
    
    	string typeP, detalP, idP;
    	unsigned int costP;
    
    
    	cout<<"\n Type: ";
    	getline(cin,typeP);
    	fflush(stdin);
    
    	cout<<"\n Detailes: ";
    	getline(cin,detalP);
    	fflush(stdin);
    	
    	cout<<"\n Cost: ";
    	cin>>costP;
    
    	cout<<"\n Product ID: ";
    	getline(cin,idP);
    
    	product insertProd(typeP, detalP, costP, idP);
    
    	Prod.insert(insertProd);
    }
    can anyone help?
    "Artificial Intelligence usually beats natural stupidity."

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Overload the += operator for grocery. Something like:
    Code:
    void grocery::operator+= (const product& p)
    {
        mySet.insert(p);
    }
    (I'm not sure if this is good use for operator overloading. A function name conveys more information about what to expect from it.)

    Note that in your existing code the set is local to the method and disappears when the function exists.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can't use += to add to a set. You use insert instead, which you seem to already be doing.

    A couple other comments about the code:
    1. You created a set call Prod and insert a product into it. But that set is a local variable that is destroyed when the function ends, so you really did nothing. Shouldn't you be inserting into a member variable of the grocery class?
    2. fflush(stdin); is bad and unnecessary. I'm not sure why you think you need it, but I'm pretty sure you don't. Remove it.
    3. You call cin>>costP; and then call getline(cin,idP); just below it. If you use cin>> and getline together, you need to add a cin.ignore() after the cin>> call to ignore the trailing newline. This should only be done after cin>>, not after getline.

  4. #4
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hey Anon

    for the operator to work, i need to insert the code i used in my function ( void grocery::insertProducts(); ) to the new +=operator function ( void grocery:perator+= (const product& p); ), right?
    "Artificial Intelligence usually beats natural stupidity."

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by IndioDoido View Post
    hi
    can anyone tel me how to insert multiple data at once in a std::set using an +=operator?
    Use std::set<T>::insert( start, end ) where start is the first element you want to insert and end is the last element to insert (assuming they're in some kind of array or iterable container).

  6. #6
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hi cpjust,
    but i need to use a +=operator :-(
    "Artificial Intelligence usually beats natural stupidity."

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Do you need to use it on the set (which is not possible) or for your class?

    If you want it for your class, then what do you want to add? I assume you want to add a product, but that means that the code that gets the data for the product from cin must be put somewhere else.

  8. #8
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    i have a class product with the elements for each product (price, type, etc...) and i need to create a simple program to insert, search, update and erase products from a set.

    but i must use a +=operator to insert several products at the same time :-(
    "Artificial Intelligence usually beats natural stupidity."

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Please give more information, perhaps an entire paragraph. Or just answer the questions asked.

    You cannot use += to insert into the standard library's set class.

    What your assignment probably wants is to add an operator+= to the grocery class or some other class that holds multiple Products. It's up to you to tell us if that's true.

  10. #10
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    I'm going to try and translate the assignment requirements:

    "The product table must support addictions of new products through a +=operator (supporting multiple addictions at once)."

    Its not easy for me, explaining myself in a different language, sorry.
    Last edited by IndioDoido; 10-31-2007 at 07:30 PM.
    "Artificial Intelligence usually beats natural stupidity."

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Ok, so that is referring to a "product table", not the standard library std::set class. That's the distinction I wanted to make.

    I'm not sure how you can add multiple products at once. That doesn't make sense to me.

    Do you have a class for your "product table"? Add an operator+= to that class. Use anon's code as an example.

    Hope that makes sense.

  12. #12
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    ok, thanks for the help, i'm going to work on that later.

    now i'm trying to find a product using .find(), but it doesn't work :-(

    Code:
    void grocery::findProduct()
    {
    	string id;
    
    	cout<<"Product ID: ";
    	cin>>id;
    
    	set <product>::const_iterator it;
    
    	if( Prod.find(id) == Prod.end() ) //set <product> Prod; is inside the grocery.h
    		cout<<"not found\n";
    	else cout<<"\nthe product type is: "<<it->getType() <<"\n";
    
    	system("pause");
    
    }
    i know the product was inserted because after insertion, i view all products to be certain.
    "Artificial Intelligence usually beats natural stupidity."

  13. #13
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hi Daved!

    thats correct, when i insert a product it has an ID, type, detail and cost.
    i'm going to try that...

    tnx again ;-)
    "Artificial Intelligence usually beats natural stupidity."

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I moved my post to your new thread... it's a new topic anyway so you can continue to discuss it there. Sorry for the confusion.

  15. #15
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    ok...
    no problem ;-)
    "Artificial Intelligence usually beats natural stupidity."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 06-03-2005, 01:13 AM
  2. Writing data to Multiple files
    By RaviRaj in forum C++ Programming
    Replies: 2
    Last Post: 01-21-2003, 12:28 PM
  3. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  4. How to input multiple groups of data
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-14-2002, 10:17 PM