Union, intersection, and difference set of set in STL

Source: Internet
Author: User

The set key is automatically sorted, and the corresponding intersection of the Union set difference sets can all use this ordered feature. The time complexity is O (m + n), m, n is the size of the two containers.

1. set_union can be used to calculate the union of two sets. It is a stable operation because the relative positions between elements do not change.

The source code is as follows:

Template <Class Identifier, class inputiterator2, class outputiterator> outputiterator set_union (extends first1, extends last1, extends first2, inputiterator2 last2, outputiterator result! = Last1 & first2! = Last2) {If (* first1 <* first2) {* result = * first1; ++ first1;} else if (* first1> * first2) {* result = * first2; + + first2;} else // equal case {* result = * first1; ++ first1; ++ first2 ;}++ result ;}return copy (first2, last2, copy (first1, last1, result ));}
2. set_intersection can be used to calculate the intersection of two sets.

The source code is as follows:

Template <Class Identifier, Class Identifier, class outputiterator> outputiterator set_intersection (Response first1, response last1, response first2, inputiterator2 last2, outputiterator result) {While (first1! = Last1 & first2! = Last2) {If (* first1 <* first2) {++ first1;} else if (* first1> * first2) {++ first2 ;} else {// * first1 = * first2, which is the intersection element * result = * first1; ++ first1; ++ first2; ++ result ;}} return result ;}

3. set_diference is used to calculate the difference set of two sets.

The source code is as follows:

Template <Class Identifier, Class Identifier, class outputiterator> outputiterator set_difference (partition first1, partition last1, partition first2, inputiterator2 last2, outputiterator result) {While (first1! = Last1 & first2! = Last2) {If (* first1 <* first2) // * first1 <* first2, which is the element of the difference set {* result = * first1; ++ first1; + + result;} else if (* first1> * first2) ++ first2; else {++ first1; ++ first2 ;}} return copy (first1, last1, result );}

Simple test:

# Include <iostream> # include <cstdlib> # include <set> # include <algorithm> # include <vector> using namespace STD; int main () {set <int> S1, s2; s1.insert (1); s1.insert (2); s1.insert (3); s2.insert (2); s2.insert (3); s2.insert (4); set <int> :: iterator iter1 = s1.begin (); set <int >:: iterator iter2 = s1.end (); set <int >:: iterator iter3 = s2.begin (); set <int> :: iterator iter4 = s2.end (); vector <int> res (10, 0); vector <int>: iterator ITER; iter = set_union (iter1, iter2, iter3, iter4, Res. begin (); // Union // iter = set_intersection (iter1, iter2, iter3, iter4, Res. begin (); // intersection // iter = set_difference (iter1, iter2, iter3, iter4, Res. begin (); // difference set res. resize (iter-res.begin (); For (iter = res. begin (); iter! = Res. End (); ++ ITER) {cout <* ITER <Endl;} system ("pause"); Return 0 ;}

Union, intersection, and difference set of set in STL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.