Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. NAME set_union - A basic set operation for constructing a sorted union. SYNOPSIS #include <algorithm> template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_union (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> OutputIterator set_union (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); DESCRIPTION The_set_union algorithm constructs a sorted union of the elements from the two ranges. It returns the end of the con- structed range. set_union is stable, which means that if an element is present in both ranges, the one from the first range is copied. The result of set_union is undefined if the result range overlaps with either of the original ranges. Note that set_union does not merge the two sorted sequences. If an element is present in both sequences, only the element from the first sequence is copied to result. (Use the merge algorithm to create an ordered merge of two sorted sequences that contains all the elements from both sequences.) set_union assumes that the sequences are sorted using the default comparison operator less than (<), unless an alter- native comparison operator (comp) is provided. COMPLEXITY At most ((last1 - first1) + (last2 - first2)) * 2 -1 comparisons are performed. EXAMPLE // // set_unin.cpp // #include <algorithm> #include <set> #include <iostream> using namespace std; int main() { //Initialize some sets int a2[6] = {2,4,6,8,10,12}; int a3[4] = {3,5,7,8}; set<int, less<int> > even(a2+0, a2+6), result, small(a3+0,a3+4); //Create an insert_iterator for result insert_iterator<set<int, less<int> > > res_ins(result, result.begin()); //Demonstrate set_union cout << "The result of:" << endl << "{"; copy(small.begin(),small.end(), ostream_iterator<int,char>(cout," ")); cout << "} union {"; copy(even.begin(),even.end(), ostream_iterator<int,char>(cout," ")); cout << "} =" << endl << "{"; set_union(small.begin(), small.end(), even.begin(), even.end(), res_ins); copy(result.begin(),result.end(), ostream_iterator<int,char>(cout," ")); cout << "}" << endl << endl; return 0; } Program Output The result of: {3 5 7 8 } union {2 4 6 8 10 12 } = {2 3 4 5 6 7 8 10 12 } WARNINGS If your compiler does not support default template parame- ters, then you always need to supply the Compare template argument and the Allocator template argument. For instance, you need to write: set<int, less<int>, allocator<int> > instead of: set<int> If your compiler does not support namespaces, then you do not need the using declaration for std. SEE ALSO includes, set, set_intersection, set_difference, set_symmetric_difference
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |