Intersection, union, and difference set of STL

Source: Internet
Author: User

STL has a standard algorithm for Set Operations. Has it been used? What do you need to pay attention to when using them? Example program? If you have not touched any of them, see the following brief introduction: intersection set_intersection, Union set_union, difference set_difference, and symmetry difference set_symeetric_difference.

Pay special attention to the four set operations mentioned here:
1. The first algorithm must ensure that the first and second sets are sorted in ascending order. The default "<" operator is used internally to compare the element size;
2. The second algorithm needs to ensure that the first and second sets are ordered. The sorting method is determined by compare and the size of the elements is compared internally using compare.

1 -- set_intersection (intersection)
template <class InputIterator1, class InputIterator2, class OutputIterator>OutputIterator set_intersection(InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,OutputIterator result);template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>OutputIterator set_intersection(InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp);

This function is used to calculate the intersection of two sets. The result set contains all elements that belong to both the first and second sets. For example, the intersection of {1, 2, 3, 7, 9} and {3, 4, 5, 7} In a set is {3, 7 }.
Function return value: the end position iterator of the result set.
Parameter: Start position of the first set, end position of the first set, start position of the second parameter, end position of the second parameter, and insert iterator of the result set. For the second algorithm, compare specifies a function that is used to compare the element size.

2 -- set_union (union)
template <class InputIterator1, class InputIterator2, class OutputIterator>OutputIterator set_union(InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,OutputIterator result);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 calculate the intersection of two sets. The result set contains all elements that belong to both the first and second sets. For example, the Union of {1, 2, 3, 7, 9} and {3, 4, 5, 7} is {1, 2, 3, 4, 5, 7 }.

3 -- set_difference (difference set)
template <class InputIterator1, class InputIterator2, class OutputIterator>OutputIterator set_difference(InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,OutputIterator result);template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>OutputIterator set_difference(InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp)

This function is used to evaluate the difference sets of two sets. The result set contains all elements that belong to the first set but do not belong to the second set. For example, the difference set of {1, 2, 3, 7, 9} and {3, 4, 5, 7} is {1, 2, 9 }.

4 -- set_symeetric_difference (symmetric difference set)
template<class InputIterator1, class InputIterator2, class OutputIterator>OutputIterator set_symmetric_difference(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,InputIterator2 last2,OutputIterator result);template<class InputIterator1, class InputIterator2, class OutputIterator, class Compare&gtOutputIterator set_symmetric_difference(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,InputIterator2 last2,OutputIterator result, Compare comp);

In mathematics, the symmetric difference set of two sets only belongs to one of the sets, and does not belong to a set composed of elements of another set. That is to say, the symmetric difference set is a set composed of other elements in the two sets after the elements that appear in the two sets are removed simultaneously. For example, the symmetry difference between {1, 2, 3, 7, 9} and {3, 4, 5, 5} in the set is {1, 2, 9 }. This operation in set theory is equivalent to the exclusive or operation in Boolean logic. The symmetry difference between A and B is usually expressed as a △b.

5-intersection, collection, difference set, and symmetric difference set usage
#include #include templatevoid print_vector(const std::string & s_name, const std::vector & vec){typedef typename std::vector::size_type size_type;std::cout << s_name << ": ";for (size_type i = 0; i < vec.size(); i++)std::cout << vec[i] << (i == vec.size() - 1?"\n":",");}int main(){int a[] = {1,2,3,7,9};std::vector v_less_1(a, a + 5);int b[] = {3,4,5,7};std::vector v_less_2(b, b + 4);std::vector vr_less;std::set_intersection(v_less_1.begin(), v_less_1.end(), v_less_2.begin(), v_less_2.end(),std::insert_iterator<std::vector >(v_less, v_less.begin()));print_vector("v_less", v_less);std::vector vr_less;std::set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), std::insert_iterator<std::vector >(v_less, v_less.begin()), std::less());print_vector("v_less", v_less);std::vector v_greater;std::set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), std::insert_iterator<std::vector >(v_greater, v_greater.begin()), std::greater());print_vector("v_greater", v_greater);return 0;}

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.