![]() |
In C++, sets are the data containers that store the unique elements in some specified order. Concatenating two sets means merging the elements of two sets into the first set. In this article, we will learn how to concatenate two sets in C++ STL. Example Input: mySet1 = {1, 2, 3, 4, 5, 8, 9} mySet2 = {1, 5, 6, 8, 9, 11, 12, 13} Output: result: {1, 2, 3, 4, 5, 6, 8, 9, 11, 12, 13} Merge Two Sets in C++To concatenate two sets in C++, we can use the std::set_union function provided in the STL <algorithm> library that finds the union of two sorted ranges and stores it in some other container. After that, we can replace the content of the first set with the resulted container. Syntax of std::set_union()set_union (first1, last1, first2, last2, result);
where,
C++ Program to Concatenate Two SetsC++
Output
Concatenated Set: 1 2 3 4 5 6 Time Complexity: O(N*logN + MlogM), where M and N is the size of two vectors. In C++ STL, you can also concatenate two sets using the insert function. |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |