c++ - std::set and its front_insert_iterator -
i know can this:
std::vector<double> vec; std::back_insert_iterator<std::vector<double> > it( back_inserter(vec) ); = 4.5;
but i'd similar (in syntax) std::set
(and use front_insert_iterator
instead of reference set , use set::insert
). possible, or forced use reference o set? maybe should use std::merge
and/or std::set_intersect
(this allow error reporting in case of duplicates)? approach?
thanks!
you don't push_back
or push_front
set
, because (conceptually) set
sorted associative container. can this, though:
#include <cstdlib> #include <set> #include <iterator> using namespace std; int main() { typedef set<int> myset; myset si; insert_iterator<myset> it(si, si.begin()); *it = 1; *it = 2; }
edit:
note begin()
iterator initialized not elements put. rather, hint stl start looking put item.
edit2:
as per comments below, wanted way check "disposition" of inserted item. is, way tell if item or not present before inserted it.
you cannot directly using only iterator. if need information, have 2 choices.
1) don't use insert iterator. way bool
set::insert
call set::insert
. call set::insert
42) check size of set
both before , after insertion. if size grew one, item inserted. :) i've market item #42 because imo less favorable calling insert
directly number of reasons. there may multithreading issues, there may performance hit in computing size()
, etc.
Comments
Post a Comment