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
Algorithm search - search checks whether the sequence in the second range
Algorithm search - search checks whether the sequence in the second range search Header <algorithm> template<class ForwardIterator1, class ForwardIterator2> ForwardIterator2 search(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2) search checks whether the sequence in the second range [first2, last2), is a subset of the sequence in the range [first1, last1). If found, it returns an iterator i in the range [first1, last1) that represents start of the subsequence, otherwise last1 is returned. The non-predicate version uses operator== for comparison. template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator2 search(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pr) search checks whether the sequence in the second range [first2, last2), is a subset of the sequence in the range [first1, last1). If found, it returns an iterator i in the range [first1, last1) that represents start of the subsequence, otherwise last1 is returned. The predicate version uses predicate function pr for comparison. Sample #pragma warning (disable :4786) #include <algorithm> #include <iostream> #include <string> #include <functional> int main() { int set1[5] = {10, 15, 20, 25, 30} ; int set2[2] = {20, 25} ; int* pos ; std::ostream_iterator<int> intOstreamIt(std::cout, ", ") ; std::cout << "set1 = " ; std::copy(set1, set1+5, intOstreamIt) ; std::cout << std::endl ; std::cout << "set2 = " ; std::copy(set2, set2+2, intOstreamIt) ; std::cout << std::endl ; //non-predicate version of search pos = std::search(set1, set1+5, set2, set2+2) ; std::cout << "\n\nusing non-predicate version of search" << std::endl ; std::cout << "match found at offset " << (pos - set1) << std::endl ; //predicate version of search pos = std::search(set1, set1+5, set2, set2+2, std::equal_to<int>()) ; std::cout << "\n\nusing predicate version of search" << std::endl ; std::cout << "match found at offset " << (pos - set1) << std::endl ; return 0 ; } Program Output set1 = 10, 15, 20, 25, 30, set2 = 20, 25, using non-predicate version of search match found at offset 2 using predicate version of search match found at offset 2
Privacy Policy
|
Link to Us
|
Links