Topic: set<> insert


Author: Ross Smith <ross.smith@nz.eds.com>
Date: 1997/09/28
Raw View
John Homrighausen wrote:
>
> I am using the vector and set sequences in the STL and can't seem to
> find what iterator the insert function returns.
>   set<int> links;
>   set<int>::interator currents = links.insert(5);
> does currents point to the element I just inserted?

Your compiler will reject that code (even without the typo). set::insert
(at least the single-argument version) doesn't return an iterator, it
returns a pair<iterator,bool>. The bool in the pair indicates whether
the insertion was successful. If it's true, then the iterator points to
the newly inserted element; if it's false, it means an identical element
was already present in the set, and the iterator points to that element.
("Identical" defined in terms of the set's comparison parameter.)

> how about with vectors:
>   vector<int> pages;
>   vector<int>::iterator currentv = pages.insert(pages.begin(), 5);

Yes, that will do as you expect: the returned iterator points to the
newly inserted element.

For containers that allow duplicate elements (vector, deque, list,
multiset, and multimap), the insert(object) method returns an iterator
pointing to the newly inserted element. For containers that don't allow
duplicate keys (set and map), it returns a pair<iterator,bool>, as
described above.

--
Ross Smith ............................. <mailto:ross.smith@nz.eds.com>
Internet and New Media, EDS (New Zealand) Ltd., Wellington, New Zealand
   "The first thing we do, let's kill all the language lawyers."
                             -- Henry VI Part II, by W. Shakespeare;
                                additional dialogue by B. Stroustrup
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: John Homrighausen <jhausen@bellsouth.com>
Date: 1997/09/25
Raw View
Hello all,
 I am using the vector and set sequences in the STL and can't seem to
find what iterator the insert function returns.
 set<int> links;
 // load set
 set<int>::interator currents = links.insert(5);
 does currents point to the element I just inserted?  how about
with vectors:  vector<int> pages;
       vector<int>::iterator currentv = pages.insert(pages.begin(), 5);
I am looking at the 12/96 draft and don't see anything that specific.
 John
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]