STL algorithm set_symmetric_difference, set_union (54)

Source: Internet
Author: User

Set_effecric_difference prototype:

STD: set_policric_difference
Default (1)
template <class InputIterator1, class InputIterator2, class OutputIterator>  OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1,                                           InputIterator2 first2, InputIterator2 last2,                                           OutputIterator result);
Custom (2)
template <class InputIterator1, class InputIterator2,          class OutputIterator, class Compare>  OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1,                                           InputIterator2 first2, InputIterator2 last2,                                           OutputIterator result, Compare comp);

This function is used to calculate the symmetry difference between two sets.

The pre-use sequence should be ordered.

The concept of Symmetric Difference is:

Mathematical terms in set theory, that is, the symmetry difference between two sets is a set composed of only one, not the elements of the other. This operation in set theory is equivalent to the XOR operation in Boolean logic. The symmetry difference between A and B is usually expressed as a △b. For example, the symmetry difference between {1, 2, 3} and {3, 4} In a set is {1, 2, 4 }. The symmetry difference between a set of all students and a set of all female students is a set of all malsexual students and all female non-students. It indicates that the symbol is △or △. The symmetry difference is equivalent to the union of two relative complementary sets, namely:

Symmetric Difference the Wen's diagram represents, and the red area represents the Symmetric Difference

A Delta B = (? B) round (B? A) can also be expressed as the union of two sets minus their intersection: A △b = (A then B )? (A branch B) -- from Baidu encyclopedia


The behavior is similar:

template <class InputIterator1, class InputIterator2, class OutputIterator>  OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1,                                           InputIterator2 first2, InputIterator2 last2,                                           OutputIterator result){  while (true)  {    if (first1==last1) return std::copy(first2,last2,result);    if (first2==last2) return std::copy(first1,last1,result);    if (*first1<*first2) { *result=*first1; ++result; ++first1; }    else if (*first2<*first1) { *result = *first2; ++result; ++first2; }    else { ++first1; ++first2; }  }}
A simple example:

#include <iostream>#include <algorithm>#include <vector>using namespace std;void setsydifference(){    vector<int> vi{1,2,3,4,4,6,9,22,11};    vector<int> v2{1,2,3,5,4,9,8};    vector<int> vresult(10);    sort(vi.begin(),vi.end());    sort(v2.begin(),v2.end());    cout<<"vi=";    for(int i:vi)        cout<<i<<" ";    cout<<endl;    cout<<"v2=";    for(int i:v2)        cout<<i<<" ";    cout<<endl;    auto it=set_symmetric_difference(vi.begin(),vi.end(),v2.begin(),v2.end(),vresult.begin());    cout<<"vreult=";    for(auto i=vresult.begin();i!=it;++i)        cout<<*i<<" ";    cout<<endl;}
Run:





Set_union prototype:

STD: set_union
Default (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);
This function is used to obtain the union of two sets.

The pre-use sequence should be in ascending order.

The behavior is similar:

template <class InputIterator1, class InputIterator2, class OutputIterator>  OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,                            InputIterator2 first2, InputIterator2 last2,                            OutputIterator result){  while (true)  {    if (first1==last1) return std::copy(first2,last2,result);    if (first2==last2) return std::copy(first1,last1,result);    if (*first1<*first2) { *result = *first1; ++first1; }    else if (*first2<*first1) { *result = *first2; ++first2; }    else { *result = *first1; ++first1; ++first2; }    ++result;  }}
A simple example:

#include <iostream>#include <algorithm>#include <vector>using namespace std;void setunion(){    vector<int> vi{1,2,3,4,4,6,9,22,11};    vector<int> v2{1,2,3,5,4,9,8};    vector<int> vresult(15);    sort(vi.begin(),vi.end());    sort(v2.begin(),v2.end());    cout<<"vi=";    for(int i:vi)        cout<<i<<" ";    cout<<endl;    cout<<"v2=";    for(int i:v2)        cout<<i<<" ";    cout<<endl;    auto it=set_union(vi.begin(),vi.end(),v2.begin(),v2.end(),vresult.begin());    cout<<"vreult=";    for(auto i=vresult.begin();i!=it;++i)        cout<<*i<<" ";    cout<<endl;}
Run:




------------------------------------------------------------------

// For more instructions on writing errors or poor information, you can leave a message below or click the email address in the upper left corner to send an email to me, pointing out my errors and deficiencies, so that I can modify them, thank you for sharing it.

Reprinted please indicate the source: http://blog.csdn.net/qq844352155

Author: unparalleled

Email: [email protected]

Yu gdut

--------------------------------------------------






STL algorithm set_symmetric_difference, set_union (54)

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.