template<class InputIterator, class T> InputIterator find (InputIterator first, InputIterator last, const T& val) { while (first!=last) { if (*first==val) return first; ++first; } return last; }
template<class InputIterator, class UnaryPredicate> InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred) { while (first!=last) { if (pred(*first)) return first; ++first; } return last; }此时可能的UnaryPredicate有,bind2nd(equal_to<int>(), 0),bind2nd(less<int>(), 0),自定义等
template <class ForwardIterator> ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last) { if (first != last) { ForwardIterator next=first; ++next; while (next != last) { if (*first == *next) // or: if (pred(*first,*next)), for version (2) return first; ++first; ++next; } } return last; }
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; }
template<class ForwardIterator1, class ForwardIterator2> ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2) { if (first2==last2) return first1; // specified in C++11 while (first1!=last1) { ForwardIterator1 it1 = first1; ForwardIterator2 it2 = first2; while (*it1==*it2) { // or: while (pred(*it1,*it2)) for version 2 ++it1; ++it2; if (it2==last2) return first1; if (it1==last1) return last1; } ++first1; } return last1; }
这个函数名字有点问题,不应该和find归在一起,应该是search_end
Searches the range [first1,last1) for the last occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element, or last1 if no occurrences are found.
template<class ForwardIterator1, class ForwardIterator2> ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2) { if (first2==last2) return last1; // specified in C++11 ForwardIterator1 ret = last1; while (first1!=last1) { ForwardIterator1 it1 = first1; ForwardIterator2 it2 = first2; while (*it1==*it2) { // or: while (pred(*it1,*it2)) for version (2) ++it1; ++it2; if (it2==last2) { ret=first1; break; } if (it1==last1) return ret; } ++first1; } return ret; }
template<class ForwardIterator, class Size, class T> ForwardIterator search_n (ForwardIterator first, ForwardIterator last, Size count, const T& val) { ForwardIterator it, limit; Size i; limit=first; std::advance(limit,std::distance(first,last)-count); while (first!=limit) { it = first; i=0; while (*it==val) // or: while (pred(*it,val)) for the pred version { ++it; if (++i==count) return first; } ++first; } return last; }
template <class InputIterator, class T> typename iterator_traits<InputIterator>::difference_type count (InputIterator first, InputIterator last, const T& val) { typename iterator_traits<InputIterator>::difference_type ret = 0; while (first!=last) { if (*first == val) ++ret; ++first; } return ret; }
template <class InputIterator, class UnaryPredicate> typename iterator_traits<InputIterator>::difference_type count (InputIterator first, InputIterator last, UnaryPredicate pred) { typename iterator_traits<InputIterator>::difference_type ret = 0; while (first!=last) { if (pred(*first)) ++ret; ++first; } return ret; }
template<class InputIterator, class Function> Function for_each(InputIterator first, InputIterator last, Function fn) { while (first!=last) { fn (*first); ++first; } return fn; // or, since C++11: return move(fn); }
template <class InputIterator1, class InputIterator2> bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 ) { while (first1!=last1) { if (!(*first1 == *first2)) // or: if (!pred(*first1,*first2)), for version 2 return false; ++first1; ++first2; } return true; }
template <class InputIterator1, class InputIterator2> pair<InputIterator1, InputIterator2> mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 ) { while ( (first1!=last1) && (*first1==*first2) ) // or: pred(*first1,*first2), for version 2 { ++first1; ++first2; } return std::make_pair(first1,first2); }
template <class InputIterator1, class InputIterator2> bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2); template <class InputIterator1, class InputIterator2, class Compare> bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp); template <class InputIterator1, class InputIterator2> bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2) { while (first1!=last1) { if (first2==last2 || *first2<*first1) return false; else if (*first1<*first2) return true; ++first1; ++first2; } return (first2!=last2); }
template <class T> const T& min (const T& a, const T& b) { return !(b<a)?a:b; // or: return !comp(b,a)?a:b; for version (2) }
template <class T> const T& max (const T& a, const T& b) { return (a<b)?b:a; // or: return comp(a,b)?b:a; for version (2) }
template <class ForwardIterator> ForwardIterator min_element ( ForwardIterator first, ForwardIterator last ) { if (first==last) return last; ForwardIterator smallest = first; while (++first!=last) if (*first<*smallest) // or: if (comp(*first,*smallest)) for version (2) smallest=first; return smallest; }
template <class ForwardIterator> ForwardIterator max_element ( ForwardIterator first, ForwardIterator last ) { if (first==last) return last; ForwardIterator largest = first; while (++first!=last) if (*largest<*first) // or: if (comp(*largest,*first)) for version (2) largest=first; return largest; }