From 2391109625836428131 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f78e5,66d20309909372f6 X-Google-Attributes: gidf78e5,public From: ajrobb@ecr.mu.oz.au (Andrew_J ROBBIE) Subject: Re: STL and set_union algorithm question? (newbie to STL!) Date: 1998/05/01 Message-ID: <6i9dgp$a2n$1@mulga.cs.mu.OZ.AU>#1/1 X-Deja-AN: 349172124 Approved: Fergus Henderson References: <6g2te9$a7n$1@ukwsv3.ggr.co.uk> X-Original-Date: 30 Apr 1998 08:42:01 GMT Organization: Computer Science, The University of Melbourne X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUANUlRHuEDnX0m9pzZAQG78AGAk7Hf0I+5995wbIxbs+jlFnoOPKwRD1VV IlrnROdLbjqOV5vNPUuj7rVuICBauelL =7Dph Newsgroups: comp.std.c++ NJ Biggs (njb1441@ggr.co.uk) wrote: : I am new to STL and am after a little help. Join the club... : Can anyone please post a short example of using the set_union algorithm on : two set objects that places the result in a third set. : Sort of... : set s1; : set s2; : set result; : set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), result.end()); Firstly, you should have the destination iterator (the 5th arg) being at the start of where you wish the output to go. Secondly, the container must have space to put the items in. Thirdly, I can't see how it can work for ordered containers. Re-forming your code: set s1; set s2; vector result(s1.size() + s2.size()); vector::const_iterator it_last; ostream_iterator it_out(cout, "\n"); it_last = set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), result.begin()); // Display the result: copy(result.begin(), it_last, it_out); // or make a new set: set s3(result.begin(), it_last); In this code, I made the destination big enough to hold the largest possible number of elements. It seems terribly kludgy though. I wish there was a better way to do this though. What I would like is to have some pseudo-iterator which knows about the whole container, so can therefore call container->insert() when the set_union() code assigns to it. In fact, I would like to be able to do what ostream_iterator does: set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), it_out); Can any gurus tell me how I can get this to occur for iterators for sets, etc ? TIA, Andrew --- [ 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 ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]