STL source code profiling algorithm set

Source: Internet
Author: User

STL source code profiling algorithm set

 

Set Algorithms

Bytes ------------------------------------------------------------------------------------
Description:
Set_union, set_difference, set_intersection, set accepted by set_symmetric_difference algorithm,
It must be an ordered interval. It is applicable to set/multiset with RB-tree as the underlying layer and not to hash_set/hash_multiset with hash as the underlying layer.
Of course, it is also applicable to other ordered intervals of non-set/multiset, as long as it is ordered.

Source code:
Set_union
Constructs the Union of S1 and S2. If a value appears n times in S1 and m times in S2, the value appears max (m, n) times in the output range.
Figure 6-5a
Template
 
  
OutputIterator set_union (inclufirst1, inclulast1, inclufirst2, InputIterator2 last2, OutputIterator result) {// when neither of the two zones has reached the end, perform the following operations while (first1! = Last1 & first2! = Last2) {// the two elements are not equal. Take a smaller element and move forward the if (* first1 <* first2) {* result = * first1; + + first1;} else if (* first2 <* first1) {* result = * first2; ++ first2;} // the two elements are equal. Take the S1 element, move the iterator of two intervals else {* result = * first1; ++ first1; ++ first2;} // move the iterator of the result interval ++ result ;} // as long as there is an interval that reaches the end, the above while loop ends, and then all the elements that do not reach the end are copied to the destination return copy (first2, last2, copy (first1, last1, result ));}
 

Set_intersection
Construct the intersection of S1 and S2. If a value appears n times in S1 and m times in S2, the value appears min (m, n) times in the output range.
Figure 6-5b
Template
 
  
OutputIterator set_intersection (interval first1, InputIterator1 last1, interval first2, interval last2, OutputIterator result) {// when both intervals have not reached the end, perform the following operations while (first1! = Last1 & first2! = Last2) // iterator that moves forward to a small element interval until there are elements with the same if (* first1 <* first2) ++ first1; else if (* first2 <* first1) + + first2; // else {* result = * first1; + + first1; ++ first2; ++ result;} return result ;}
 

Set_difference
Construct the difference set of S1 and S2. If a value appears n times in S1 and m times in S2, the value appears min (n-m, 0) times in the output range.
Figure 6-5c
Template
 
  
OutputIterator set_difference (interval first1, InputIterator1 last1, interval first2, interval last2, OutputIterator result) {// when both intervals have not reached the end, perform the following operations while (first1! = Last1 & first2! = Last2) // when the S1 element is smaller than S2, take the S1 element if (* first1 <* first2) {* result = * first1; ++ first1; ++ result ;} // when the S1 element is greater than the S2 element, move forward the S2 iterator else if (* first2 <* first1) + + first2; // when the S1 and S2 elements are equal, moving forward S1 and S2 iterators else {++ first1; ++ first2;} // If S2 ends first, copy the rest of S1 elements return copy (first1, last1, result );}
 

Set_effecric_difference --> it appears in S1 but does not appear in S2 and s2. It does not appear in S1.
Construct the symmetric difference set of S1 and S2. If a value appears n times in S1 and m times in S2, this value appears in the output interval | n-m | times

Figure 6-5d

 

Template
 
  
OutputIterator initialize (InputIterator1 first1, InputIterator1 last1, interval first2, interval last2, OutputIterator result) {// when the two zones have not reached the end, perform the following operations while (first1! = Last1 & first2! = Last2) // when the element of S1 is smaller than S2, take the element of S1 if (* first1 <* first2) {* result = * first1; ++ first1; + + result;} // when the element S2 is smaller than the element S1, take the element else if (* first2 <* first1) of S2 {* result = * first2; ++ first2; + + result;} // when both elements are equal, else {++ first1; ++ first2;} return copy (first2, last2, copy (first1, last1, result ));}
 


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.