标签:style blog http color io for
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
1.iter_swap
描述:将两个 ForwardIterator 所指的对象对调
源码:
//version 1 template <class ForwardIterator1, class ForwardIterator2, class T> inline void __iter_swap(ForwardIterator1 a, ForwardIterator2 b, T*) { T tmp = *a; *a = *b; *b = tmp; } //version 2 template <class ForwardIterator1, class ForwardIterator2> inline void iter_swap(ForwardIterator1 a, ForwardIterator2 b) { __iter_swap(a, b, value_type(a)); } //version 3 template <class T> inline void swap(T& a, T& b) { T tmp = a; a = b; b = tmp; }
template <class T> inline const T& min(const T& a, const T& b) { return b < a ? b : a; } template <class T> inline const T& max(const T& a, const T& b) { return a < b ? b : a; } template <class T, class Compare> inline const T& min(const T& a, const T& b, Compare comp) { return comp(b, a) ? b : a; } template <class T, class Compare> inline const T& max(const T& a, const T& b, Compare comp) { return comp(a, b) ? b : a; }
template <class ForwardIterator, class T> void fill(ForwardIterator first, ForwardIterator last, const T& value) { for ( ; first != last; ++first) *first = value; } template <class OutputIterator, class Size, class T> OutputIterator fill_n(OutputIterator first, Size n, const T& value) { for ( ; n > 0; --n, ++first) *first = value; return first; }
//version 1 template <class InputIterator1, class InputIterator2> pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2) { while (first1 != last1 && *first1 == *first2) { ++first1; ++first2; } return pair<InputIterator1, InputIterator2>(first1, first2); } //version 2 template <class InputIterator1, class InputIterator2, class BinaryPredicate> pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate binary_pred) { while (first1 != last1 && binary_pred(*first1, *first2)) { ++first1; ++first2; } return pair<InputIterator1, InputIterator2>(first1, first2); }
//version 1 template <class InputIterator1, class InputIterator2> inline bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2) { for ( ; first1 != last1; ++first1, ++first2) if (*first1 != *first2) return false; return true; } //version 2 template <class InputIterator1, class InputIterator2, class BinaryPredicate> inline bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate binary_pred) { for ( ; first1 != last1; ++first1, ++first2) if (!binary_pred(*first1, *first2)) return false; return true; }
//version 1 template <class InputIterator1, class InputIterator2> bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2) { for ( ; first1 != last1 && first2 != last2; ++first1, ++first2) { if (*first1 < *first2) return true; if (*first2 < *first1) return false; } return first1 == last1 && first2 != last2; } //version 2 template <class InputIterator1, class InputIterator2, class Compare> bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp) { for ( ; first1 != last1 && first2 != last2; ++first1, ++first2) { if (comp(*first1, *first2)) return true; if (comp(*first2, *first1)) return false; } return first1 == last1 && first2 != last2; } //version 3 针对原生指针 inline bool lexicographical_compare(const unsigned char* first1, const unsigned char* last1, const unsigned char* first2, const unsigned char* last2) { const size_t len1 = last1 - first1; const size_t len2 = last2 - first2; const int result = memcmp(first1, first2, min(len1, len2)); return result != 0 ? result < 0 : len1 < len2; } //version 4 inline bool lexicographical_compare(const char* first1, const char* last1, const char* first2, const char* last2) { #if CHAR_MAX == SCHAR_MAX return lexicographical_compare((const signed char*) first1, (const signed char*) last1, (const signed char*) first2, (const signed char*) last2); #else return lexicographical_compare((const unsigned char*) first1, (const unsigned char*) last1, (const unsigned char*) first2, (const unsigned char*) last2); #endif }
STL 源码剖析 stl_algobase.h,布布扣,bubuko.com
标签:style blog http color io for
原文地址:http://blog.csdn.net/zhengsenlie/article/details/37882323