blob: e9be3ae46c4f26d969c0eb9dcb45824de74c3996 [file] [log] [blame]
/**************************************************************************
*
* set_union.cpp - Example program of set union algorithm.
*
* $Id: //stdlib/dev/examples/stdlib/manual/set_union.cpp#5 $
*
***************************************************************************
*
* Copyright (c) 1994-2005 Quovadx, Inc., acting through its Rogue Wave
* Software division. Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0. Unless required by
* applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under
* the License.
*
**************************************************************************/
#include <algorithm>
#include <set>
#include <iostream>
#include <iterator>
int main ()
{
typedef std::set<int, std::less<int>, std::allocator<int> > Set;
// Initialize sets.
const int a1[] = { 2, 4, 6, 8, 10, 12 };
const int a2[] = { 3, 5, 7, 8 };
const Set s1 (a1, a1 + sizeof a1 / sizeof *a1);
const Set s2 (a2, a2 + sizeof a2 / sizeof *a2);
Set result;
// Create an insert_iterator for result.
std::insert_iterator<Set> res_ins (result, result.begin ());
// Compute union of two sets.
std::set_union (s2.begin (), s2.end (), s1.begin (), s1.end (), res_ins);
// Display result on standard output.
typedef std::ostream_iterator<int, char, std::char_traits<char> > OSIter;
std::cout << "The result of:\n{ ";
std::copy (s2.begin (),s2.end (), OSIter (std::cout," "));
std::cout << "} union { ";
std::copy (s1.begin (),s1.end (), OSIter (std::cout," "));
std::cout << "} =\n{ ";
std::copy (result.begin (),result.end (), OSIter (std::cout," "));
std::cout << "}\n\n";
return 0;
}