C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ Algorithm set_union()C++ Algorithm set_union() function is used to find the union of two sorted ranges [first1, last1) and [first2, last2), which is formed by the elements that are present in either one of the sets or in both. Elements are compared using operator < for the first version or using the given binary comparison function comp for the second version. Syntaxdefault (1) template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_union (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); custom (2) template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> OutputIterator set_union (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); Parameterfirst1: An input iterator pointing to the first element in the first of two sorted source sequence. last1: An input iterator pointing to the past last element in the first of two sorted source sequence. first2: An input iterator pointing to the first element in the second sorted source sequence. last2: An input iterator pointing to the past last element in the second sorted source sequence. comp: A user-defined binary predicate function that accepts two arguments and returns true if the two arguments are in order and false otherwise. It follows the strict weak ordering to order the elements. result: An output iterator addressing to the position of the first element in the destination range . Return valueThis function returns an iterator to the end of the constructed range. ComplexityComplexity is linear in the distance between [first1, last1) and [first2, last2): performs up to 2*(count1+count2)-1 comparisons. Where count1 = last1- first1 and count2 = last2- first2. Data racesThe object in the range [first1, last1) and [first2. last2) are accessed. The object in the range between result and returned value are modified. ExceptionsThis function throws an exception if any of element comparison, the element assignments or an operation on iterator throws an exception. Note: The invalid parameters cause an undefined behavior.Example 1Let's see the simple example to demonstrate the use of set_union(): #include <iostream> #include <set> #include <list> #include <vector> #include <algorithm> #include <iterator> using namespace std; int main() { list<int> a = {1, 2, 3, 4}; multiset<int> b = {4, 5, 6, 2}; vector<int> result; set_union(begin(a), end(a), begin(b), end(b), inserter(result, end(result))); for_each(begin(result), end(result), [](int x) { cout << x << endl; }); return 0; } Output: 1 2 3 4 5 6 Example 2Let's see another simple example: #include <iostream> // std::cout #include <algorithm> // std::set_union, std::sort #include <vector> // std::vector using namespace std; int main() { int first[] = { 5, 10, 15, 20, 25 }; int second[] = { 50, 40, 30, 20, 10 }; int n = sizeof(first) / sizeof(first[0]); // Print first array cout << "First array contains:"; for (int i = 0; i < n; i++) cout << " " << first[i]; cout << "\n"; // Print second array cout << "Second array contains:"; for (int i = 0; i < n; i++) cout << " " << second[i]; cout << "\n\n"; vector<int> v(10); vector<int>::iterator it, st; sort(first, first + n); sort(second, second + n); // Using default function it = set_union(first, first + n, second, second + n, v.begin()); cout << "The union has " << (it - v.begin()) << " elements:\n"; for (st = v.begin(); st != it; ++st) cout << ' ' << *st; cout << '\n'; return 0; } Output: First array contains: 5 10 15 20 25 Second array contains: 50 40 30 20 10 The union has 8 elements: 5 10 15 20 25 30 40 50 Example 3Let's see another simple example to find the list of all students who are attending both subjects: #include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; // Driver code int main() { string first[] = { "Nikita", "Divya", "Deep", "Kashish" }; string second[] = { "Aman", "Nikita", "Amita", "Deep" }; int n = sizeof(first) / sizeof(first[0]); // Print students of first list cout << "Students in first subject:"; for (int i = 0; i < n; i++) cout << " " << first[i]; cout << "\n"; // Print students of second list cout << "Students in second subject:"; for (int i = 0; i < n; i++) cout << " " << second[i]; cout << "\n\n"; vector<string> v(10); vector<string>::iterator it, st; // Sorting both the list sort(first, first + n); sort(second, second + n); // Using default operator< it = set_union(first, first + n, second, second + n, v.begin()); cout << "Students attending both subjects are:\n"; for (st = v.begin(); st != it; ++st) cout << ' ' << *st; cout << '\n'; return 0; } Output: Students in first subject: Nikita Divya Deep Kashish Students in second subject: Aman Nikita Amita Deep Students attending both subjects are: Aman Amita Deep Divya Kashish Nikita Example 4Let's see a simple example: #include <vector> #include <iostream> #include <algorithm> #include <iterator> using namespace std; int main() { vector<char> v1 = {'A', 'B', 'C'}; vector<char> v2 = { 'C', 'D', 'E', 'F'}; vector<char> dest1; set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(dest1)); for (const auto &i : dest1) { cout << i << ' '; } cout << '\n'; return 0; } Output: A B C D E F
Next TopicC++ Algorithm
|