Fusion algorithm of Stl_algorithm algorithm

Source: Internet
Author: User

Fusion algorithm:

7.64, Template <class InputIterator1, class InputIterator2, class Outputiterator>outputiterator merge ( 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); *result++ = (*first2 < *first1)? *first2++: *first1++;  Use op< for comparison sorting. }}//the first interval and the second interval are fused, and the result after fusion is sorted.

7.65, Inplace_merge//merger

7.66, Template <class InputIterator1, class Inputiterator2>bool includes (InputIterator1 first1, InputIterator1 Last1,inputiterator2 First2, InputIterator2 last2) {while (FIRST2!=LAST2) {if ((first1==last1) | | (*first2 < *first1))    return false; if (! (    *FIRST1<*FIRST2)) ++first2;  ++first1; } return true; The thing to do is to determine whether the first interval is contained in the second interval. The premise is that these two intervals are already sorted.

7.67, Template <class InputIterator1, class InputIterator2, class Outputiterator>outputiterator set_union ( InputIterator1 First1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OUTP    Utiterator 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; } return result; The thing to do is: merge the two intervals to the right side of the sequence.

7.68, Template <class InputIterator1, class InputIterator2, class Outputiterator>outputiterator Set_intersection                   (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Outputiterator result) {while (First1!=last1 && first2!=last2) {if (*first1 < *first2) ++first    1;    else if (*first2 < *first1) ++first2;      else {*result = *first1; ++result; ++first1;    ++first2; }} return result; The thing to do is to get the intersection of two intervals and return this set.

7.69, Template <class InputIterator1, class InputIterator2, class Outputiterator>outputiterator set_difference (                  InputIterator1 First1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Outputiterator result) {while (First1!=last1 && first2!=last2) {if (*first1<*first2) {*resul t = *first1; ++result;     ++first1;    } else if (*first2<*first1) ++first2;    else {++first1; ++first2; }} return Std::copy (First1,last1,result);} The thing to do is to get the element that is different from the second interval in the first interval.

7.70, Template <class InputIterator1, class InputIterator2, class Outputiterator>outputiterator Set_symmetric_ Difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last 2, outputiterator result) {while (true) {if (First1==last1) return std::copy (first2,last2,res    ULT);    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; The thing}}//did was to get the different elements in the two intervals.

Fusion algorithm of Stl_algorithm algorithm

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.