C++98 | equality (1) | template <class ForwardIterator1, class ForwardIterator2> ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2); |
predicate(2) | template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred); |
template<class InputIterator, class ForwardIterator> InputIterator find_first_of ( InputIterator first1, InputIterator last1, ForwardIterator first2, ForwardIterator last2) { while (first1!=last1) { for (ForwardIterator it=first2; it!=last2; ++it) { if (*it==*first1) // or: if (pred(*it,*first)) for version (2) return first1; } ++first1; } return last1; } |
// find_first_of example #include <iostream> // std::cout #include <algorithm> // std::find_first_of #include <vector> // std::vector #include <cctype> // std::tolower bool comp_case_insensitive (char c1, char c2) { return (std::tolower(c1)==std::tolower(c2)); } int main () { int mychars[] = {'a','b','c','A','B','C'}; std::vector<char> haystack (mychars,mychars+6); std::vector<char>::iterator it; int needle[] = {'A','B','C'}; // using default comparison: it = find_first_of (haystack.begin(), haystack.end(), needle, needle+3); if (it!=haystack.end()) std::cout << "The first match is: " << *it << '\n'; // using predicate comparison: it = find_first_of (haystack.begin(), haystack.end(), needle, needle+3, comp_case_insensitive); if (it!=haystack.end()) std::cout << "The first match is: " << *it << '\n'; return 0; } |
search | 搜索子序列的范围(函数模板) |
find | 在范围内查找值(函数模板) |
find_if | 在范围内查找元素(函数模板) |