[C + + standard library]_[primary]_[intersection and complement set]

Source: Internet
Author: User


Scene:

1. Calculate the same element in Std::vector A and std::vector B, for the reservation is not deleted.

2. Calculate the respective complement of std::vector A and std::vector B to delete the complement of a and add a complement of B, used in some operations to update the associated table. For example, contact a belongs to Group B is a set of BV, the group of contact a belongs to

To modify the CV of the collection, you need to delete the CV complement set of the two collection BV,CV and the new BV complement.

3. The C + + standard library provides these algorithms for us.


Code:

Test_androidassistant.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <algorithm> #include <vector> #include <string> #include <iostream > #include "gtest/gtest.h" TEST (test_androidassistant,setintersection) {std::vector<int> V1;v1.push_back (3 ); V1.push_back (121), V1.push_back (5);std::vector<int> v2;v2.push_back (2); V2.push_back (); V2.push_back (3); V2.push_back (5); Std::sort (V1.begin (), V1.end ()), Std::sort (V2.begin (), V2.end ());std::vector<int> RESULT;STD :: Set_intersection (V1.begin (), V1.end (), V2.begin (), V2.end (), std::back_inserter (result); Std::cout << "set_ Intersection "<< std::endl;for (int i = 0; i< result.size (); ++i) {std::cout << result[i] << Std::endl; }std::vector<int> d1;std::vector<int> d2;std::cout << "set_different" << std::endl;//copies  The elements from the sorted range [First1, Last1)//which is not found in the sorted range [First2, Last2] to the Range beginning at d_firststd::set_Difference (V1.begin (), V1.end (), Result.begin (), Result.end (), Std::back_inserter (D1)); Std::set_difference ( V2.begin (), V2.end (), Result.begin (), Result.end (), Std::back_inserter (D2)); Std::cout << "Set_difference D1" << std::endl;for (int i = 0; i< d1.size (); ++i) {std::cout << d1[i] << Std::endl;} Std::cout << "Set_difference D2" << std::endl;for (int i = 0; i< d2.size (); ++i) {std::cout << d2[i] &l t;< Std::endl;}} int _tmain (int argc, _tchar* argv[]) {testing::initgoogletest (&AMP;ARGC, argv); Run_all_tests (); return 0;}

Output:

Set_intersection35set_differentset_difference d1121set_difference d2289

Note: Here Std::back_inserter is the creation of the output enumeration type.

Look at the implementation of the set_intersection is very ingenious:

Template<class InputIt1, Class InputIt2, Class Outputit>outputit set_intersection (InputIt1 first1, InputIt1 last1 ,                          InputIt2 first2, InputIt2 last2,                          outputit d_first) {while    (first1! = Last1 && First2! = last2) {
   
    if (*first1 < *first2) {            ++first1;        } else  {            if (! ( *first2 < *first1) {                *d_first++ = *first1++;            }            ++first2;        }    }    return D_first;}
   

And look at the set_difference implementation is ingenious:

Template<class InputIt1, Class InputIt2, Class Outputit>outputit set_difference (InputIt1 first1, InputIt1 last1,< C0/>inputit2 First2, InputIt2 last2,                        outputit d_first) {while    (first1! = last1) {        if (first2 = = Last2) return Std::copy (First1, Last1, D_first);         if (*first1 < *first2) {            *d_first++ = *first1++;        } else {            if (! (*first2 < *first1)) {                ++first1;            }            ++first2;        }    }    return D_first;}






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[C + + standard library]_[primary]_[intersection and complement set]

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.