From -7717971461655220338
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,66d20309909372f6
X-Google-Attributes: gidf78e5,public
From: asbinn@rstcorp.com
Subject: Re: STL and set_union algorithm question? (newbie to STL!)
Date: 1998/05/02
Message-ID: <6icnp9$g07$1@nnrp1.dejanews.com>#1/1
X-Deja-AN: 349501509
Approved: Fergus Henderson <fjh@cs.mu.oz.au>
References: <6g2te9$a7n$1@ukwsv3.ggr.co.uk> <6i9dgp$a2n$1@mulga.cs.mu.OZ.AU>#1/1
X-Original-Date: Fri, 01 May 1998 09:55:36 -0600
X-Http-User-Agent: Mozilla/4.04 [en] (WinNT; I)
Organization: Deja News - The Leader in Internet Discussion
X-Article-Creation-Date: Fri May 01 14:55:36 1998 GMT
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUANUqLk+EDnX0m9pzZAQHeegF/ca3i4t6T/1csYdi4rATFWxltNwourUoc HjPztu4bJaHLKF5ViV27r4fkmxW69yJW =ttge
Newsgroups: comp.std.c++


In article <6i9dgp$a2n$1@mulga.cs.mu.OZ.AU>#1/1,
  ajrobb@ecr.mu.oz.au (Andrew_J ROBBIE) wrote:
>
> NJ Biggs (njb1441@ggr.co.uk) wrote:
>
   [-cut-]
> 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.
   [-cut-]
> 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 ?

What you are looking for are "instert iterators".  These iterators know
how to insert elements into a container.  There are back insert iterators
(which basically do a push_back()), front insert iterators, and plain
ol' insert iterators.

Re-forming your code:
    std::set<int> s1;
    std::set<int> s2;
    std::set<int> result_set;
    std::ostream_iterator it_out( std::cout, "\n" );

    std::set_union( s1.begin(), s1.end(), s2.begin(), s2.end(),
                    std::back_inserter( result_set ) );

    copy(result.begin(), result.end(), it_out);

If you don't want to put the result of set_union into a temporary
set, but would rather send the results directly to a ostream, then
you can just use it_out instead of the back insert iterator:

    std::set_union( s1.begin(), s1.end(), s2.begin(), s2.end(),
                    it_out );

The set algorithms in the STL are very useful.  I wrote a little command
line utility that takes two files, reads them each into a set of strings
(one line per string), then performs a set algorithm (specified on the
command line) and sends the result to std::cout.

I have some data files and I want to see the intersection, difference, etc.
of the data in the files.  I found it easier to write a little C++ program
to solve this problem then importing the data into Excel and munging the
data in there.  I found it easier to write this utility in C++ rather than
a scripting language (such as Perl) mainly becuase of the set alogirthms
in C++.


Aaron



-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading
---
[ 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              ]



