C++ Codes
Algorithms
Algorithm Analysis in C++
Beginners
Code Snippets
Graphics
Data Structures
File Manipulation
Games
Mathematics
Miscellaneous
Visual C++ Library
C++ > Visual C++ 5.0 Standard C++ Library sample source codes
The intersection of two sets is the set of elements they have in common.
The intersection of two sets is the set of elements they have in common. set_intersection Header <algorithm> template<class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_intersection(InputIterator1 first1, InputIterator1 last1, InputIterator first2, InputIterator2 last2, OutputIterator result) The intersection of two sets is the set of elements they have in common. The first set is represented by the range [first1, last1). The second set is represented by the range [first2, last2). The resulting set is placed in the range starting at result. The non-predicate version uses operator< for comparison. template<class InputIterator1, class InputIterator2, class OutputIterator, class BinaryPredicate> OutputIterator set_intersection(InputIterator1 first1, InputIterator1 last1, InputIterator first2, InputIterator2 last2, OutputIterator result, BinaryPredicate pr) The intersection of two sets is the set of elements they have in common. The first sequence is represented by the range [first1, last1). The second sequence is represented by the range [first2, last2). The resulting sequence is placed in the range starting at result. The predicate version uses the predicate function pr for comparison. Sample #pragma warning(disable : 4786) #include <algorithm> #include <iostream> #include <functional> #include <cstring> int main() { char *Alphabet = "abcdefghijklmnopqrstuvwxyz" ; char *Vowels = "aeiou" ; char *AlphaNum = "1234567890abcdef" ; char result[45] ; char *last ; int lenA = strlen(Alphabet) ; int lenV = strlen(Vowels) ; int lenAN = strlen(AlphaNum) ; std::cout << "Alphabet = " << Alphabet << std::endl ; std::cout << "\nVowels = " << Vowels << std::endl ; std::cout << "\nAlphaNum = " << AlphaNum << std::endl ; std::cout << "\n\nusing non-predicate versions" << std::endl ; //non-predicate set_difference last = std::set_difference(Alphabet, Alphabet+lenA, AlphaNum, AlphaNum+lenAN, result) ; *last = 0 ; std::cout << "\nset_difference(Alphabet, AlphaNum) = " << result << std::endl ; //non-predicate set_intersection last = std::set_intersection(Alphabet, Alphabet+lenA, AlphaNum, AlphaNum+lenAN, result) ; *last = 0 ; std::cout << "\nset_intersection(Alphabet, AlphaNum) = " << result << std::endl ; //non-predicate set_symmetric_difference last = std::set_symmetric_difference(Alphabet, Alphabet+lenA, Vowels, Vowels+lenAN, result) ; *last = 0 ; std::cout << "\nset_symmetric_difference(Alphabet, Vowels) = " << result << std::endl ; //non-predicate set_union last = std::set_union(Alphabet, Alphabet+lenA, AlphaNum, AlphaNum+lenAN, result) ; *last = 0 ; std::cout << "\nset_union(Alphabet, AlphaNum) = " << result << std::endl ; std::cout << "\n\nusing predicate versions" << std::endl ; //predicate set_difference last = std::set_difference(Alphabet, Alphabet+lenA, AlphaNum, AlphaNum+lenAN, result, std::less<char>()) ; *last = 0 ; std::cout << "\nset_difference(Alphabet, AlphaNum) = " << result << std::endl ; //predicate set_intersection last = std::set_intersection(Alphabet, Alphabet+lenA, AlphaNum, AlphaNum+lenAN, result, std::less<char>()) ; *last = 0 ; std::cout << "\nset_intersection(Alphabet, AlphaNum) = " << result << std::endl ; //predicate set_symmetric_difference last = std::set_symmetric_difference(Alphabet, Alphabet+lenA, Vowels, Vowels+lenAN, result, std::less<char>()) ; *last = 0 ; std::cout << "\nset_symmetric_difference(Alphabet, Vowels) = " << result << std::endl ; //predicate set_union last = std::set_union(Alphabet, Alphabet+lenA, AlphaNum, AlphaNum+lenAN, result, std::less<char>()) ; *last = 0 ; std::cout << "\nset_union(Alphabet, AlphaNum) = " << result << std::endl ; return 0 ; } Program Output Alphabet = abcdefghijklmnopqrstuvwxyz Vowels = aeiou AlphaNum = 1234567890abcdef using non-predicate versions set_difference(Alphabet, AlphaNum) = ghijklmnopqrstuvwxyz set_intersection(Alphabet, AlphaNum) = abcdef set_symmetric_difference(Alphabet, Vowels) = bcdfghjklmnpqrst set_union(Alphabet, AlphaNum) = 1234567890abcdefghijklmnopqrstuvwxyz using predicate versions set_difference(Alphabet, AlphaNum) = ghijklmnopqrstuvwxyz set_intersection(Alphabet, AlphaNum) = abcdef set_symmetric_difference(Alphabet, Vowels) = bcdfghjklmnpqrst set_union(Alphabet, AlphaNum) = 1234567890abcdefghijklmnopqrstuvwxyz
Privacy Policy
|
Link to Us
|
Links