Was insert successful for set container? 
Author Message
 Was insert successful for set container?

Hi

If I am using an STL set container and inserting objects into it using:

set_items.insert(item);

how can I tell if the insertion was successful or not? If the item already
exists the insert would not occur but how do I tell? What does insert
return? Can I tell what position it was inserted?

Cheers

Mike



Sun, 03 Apr 2005 23:43:15 GMT  
 Was insert successful for set container?

Quote:
> Hi

> If I am using an STL set container and inserting objects into it using:

> set_items.insert(item);

> how can I tell if the insertion was successful or not? If the item already
> exists the insert would not occur but how do I tell? What does insert
> return? Can I tell what position it was inserted?

You should pick up a copy of The C++ Standard Library: A Tutorial and
Reference, by Nicolai M. Josuttis.  It's hard to learn C++ from newsgroups.

http://www.bookpool.com/.x/o4zic9dk4n/sm/0201379260

The answer is:

// presumes set_items is std::set<int>
std::pair<std::set<int>::iterator,bool> result = set_items.insert(item);

// was it successful?
if (result.second) // ...

// where did it put it?
std::set<int>::iterator it = result.first;



Mon, 04 Apr 2005 00:10:08 GMT  
 Was insert successful for set container?
Thanks for that Reginald...I'll try it out. I'll look into the book as well.
Also you mentioned in a reply to an earlier post I made that I should try
set instead of vector. Well I did as you suggested and the time the process
took went from about 4.5 minutes to 3 seconds!! That's great...thanks again.

Mike


Quote:

message

> > Hi

> > If I am using an STL set container and inserting objects into it using:

> > set_items.insert(item);

> > how can I tell if the insertion was successful or not? If the item
already
> > exists the insert would not occur but how do I tell? What does insert
> > return? Can I tell what position it was inserted?

> You should pick up a copy of The C++ Standard Library: A Tutorial and
> Reference, by Nicolai M. Josuttis.  It's hard to learn C++ from
newsgroups.

> http://www.bookpool.com/.x/o4zic9dk4n/sm/0201379260

> The answer is:

> // presumes set_items is std::set<int>
> std::pair<std::set<int>::iterator,bool> result = set_items.insert(item);

> // was it successful?
> if (result.second) // ...

> // where did it put it?
> std::set<int>::iterator it = result.first;



Mon, 04 Apr 2005 01:43:30 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. performance issue in using set::find and set::insert with comparision function specified

2. Insert Using ADO/VC++: What am I missing?

3. Inserting some junk in the place of AM/PM

4. Cannot insert Word 2002 doc into OLE container

5. OLE Container - Insert Object

6. Setzen Papierformaten am Drucker / Setting of Paperformats on Printer

7. I am having problems setting up that a specified dialog is displayed before any others

8. Setzen Papierformaten am Drucker / Setting of Paperformats on Printer

9. STL set container

10. STL setting up vector containers?

11. Diffrent ways of useing std::set or any template container

12. 'set' container iterator question

 

 
Powered by phpBB® Forum Software