📄 stl_algo.h
字号:
{ typedef typename std::iterator_traits<_RandomAccessIter>::difference_type _DistanceType; _DistanceType __tailSize = __last - __first; const _DistanceType __pattSize = __count; if (__tailSize < __pattSize) return __last; const _DistanceType __skipOffset = __pattSize - 1; _RandomAccessIter __lookAhead = __first + __skipOffset; __tailSize -= __pattSize; while (1) // the main loop... { // __lookAhead here is always pointing to the last element of next // possible match. while (!__binary_pred(*__lookAhead, __val)) // the skip loop... { if (__tailSize < __pattSize) return __last; // Failure __lookAhead += __pattSize; __tailSize -= __pattSize; } _DistanceType __remainder = __skipOffset; for (_RandomAccessIter __backTrack = __lookAhead - 1; __binary_pred(*__backTrack, __val); --__backTrack) { if (--__remainder == 0) return (__lookAhead - __skipOffset); // Success } if (__remainder > __tailSize) return __last; // Failure __lookAhead += __remainder; __tailSize -= __remainder; } } /** * @brief Search a sequence for a number of consecutive values using a * predicate. * @param first A forward iterator. * @param last A forward iterator. * @param count The number of consecutive values. * @param val The value to find. * @param binary_pred A binary predicate. * @return The first iterator @c i in the range @p [first,last-count) * such that @p binary_pred(*(i+N),val) is true for each @c N in the * range @p [0,count), or @p last if no such iterator exists. * * Searches the range @p [first,last) for @p count consecutive elements * for which the predicate returns true. */ template<typename _ForwardIterator, typename _Integer, typename _Tp, typename _BinaryPredicate> _ForwardIterator search_n(_ForwardIterator __first, _ForwardIterator __last, _Integer __count, const _Tp& __val, _BinaryPredicate __binary_pred) { // concept requirements __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, typename iterator_traits<_ForwardIterator>::value_type, _Tp>) __glibcxx_requires_valid_range(__first, __last); if (__count <= 0) return __first; if (__count == 1) { while (__first != __last && !__binary_pred(*__first, __val)) ++__first; return __first; } return std::__search_n(__first, __last, __count, __val, __binary_pred, std::__iterator_category(__first)); } /** * @brief Swap the elements of two sequences. * @param first1 A forward iterator. * @param last1 A forward iterator. * @param first2 A forward iterator. * @return An iterator equal to @p first2+(last1-first1). * * Swaps each element in the range @p [first1,last1) with the * corresponding element in the range @p [first2,(last1-first1)). * The ranges must not overlap. */ template<typename _ForwardIterator1, typename _ForwardIterator2> _ForwardIterator2 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2) { // concept requirements __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< _ForwardIterator1>) __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< _ForwardIterator2>) __glibcxx_function_requires(_ConvertibleConcept< typename iterator_traits<_ForwardIterator1>::value_type, typename iterator_traits<_ForwardIterator2>::value_type>) __glibcxx_function_requires(_ConvertibleConcept< typename iterator_traits<_ForwardIterator2>::value_type, typename iterator_traits<_ForwardIterator1>::value_type>) __glibcxx_requires_valid_range(__first1, __last1); for ( ; __first1 != __last1; ++__first1, ++__first2) std::iter_swap(__first1, __first2); return __first2; } /** * @brief Perform an operation on a sequence. * @param first An input iterator. * @param last An input iterator. * @param result An output iterator. * @param unary_op A unary operator. * @return An output iterator equal to @p result+(last-first). * * Applies the operator to each element in the input range and assigns * the results to successive elements of the output sequence. * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the * range @p [0,last-first). * * @p unary_op must not alter its argument. */ template<typename _InputIterator, typename _OutputIterator, typename _UnaryOperation> _OutputIterator transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __unary_op) { // concept requirements __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, // "the type returned by a _UnaryOperation" __typeof__(__unary_op(*__first))>) __glibcxx_requires_valid_range(__first, __last); for ( ; __first != __last; ++__first, ++__result) *__result = __unary_op(*__first); return __result; } /** * @brief Perform an operation on corresponding elements of two sequences. * @param first1 An input iterator. * @param last1 An input iterator. * @param first2 An input iterator. * @param result An output iterator. * @param binary_op A binary operator. * @return An output iterator equal to @p result+(last-first). * * Applies the operator to the corresponding elements in the two * input ranges and assigns the results to successive elements of the * output sequence. * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each * @c N in the range @p [0,last1-first1). * * @p binary_op must not alter either of its arguments. */ template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator, typename _BinaryOperation> _OutputIterator transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _OutputIterator __result, _BinaryOperation __binary_op) { // concept requirements __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, // "the type returned by a _BinaryOperation" __typeof__(__binary_op(*__first1,*__first2))>) __glibcxx_requires_valid_range(__first1, __last1); for ( ; __first1 != __last1; ++__first1, ++__first2, ++__result) *__result = __binary_op(*__first1, *__first2); return __result; } /** * @brief Replace each occurrence of one value in a sequence with another * value. * @param first A forward iterator. * @param last A forward iterator. * @param old_value The value to be replaced. * @param new_value The replacement value. * @return replace() returns no value. * * For each iterator @c i in the range @p [first,last) if @c *i == * @p old_value then the assignment @c *i = @p new_value is performed. */ template<typename _ForwardIterator, typename _Tp> void replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value) { // concept requirements __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< _ForwardIterator>) __glibcxx_function_requires(_EqualOpConcept< typename iterator_traits<_ForwardIterator>::value_type, _Tp>) __glibcxx_function_requires(_ConvertibleConcept<_Tp, typename iterator_traits<_ForwardIterator>::value_type>) __glibcxx_requires_valid_range(__first, __last); for ( ; __first != __last; ++__first) if (*__first == __old_value) *__first = __new_value; } /** * @brief Replace each value in a sequence for which a predicate returns * true with another value. * @param first A forward iterator. * @param last A forward iterator. * @param pred A predicate. * @param new_value The replacement value. * @return replace_if() returns no value. * * For each iterator @c i in the range @p [first,last) if @p pred(*i) * is true then the assignment @c *i = @p new_value is performed. */ template<typename _ForwardIterator, typename _Predicate, typename _Tp> void replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value) { // concept requirements __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< _ForwardIterator>) __glibcxx_function_requires(_ConvertibleConcept<_Tp, typename iterator_traits<_ForwardIterator>::value_type>) __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, typename iterator_traits<_ForwardIterator>::value_type>) __glibcxx_requires_valid_range(__first, __last); for ( ; __first != __last; ++__first) if (__pred(*__first)) *__first = __new_value; } /** * @brief Copy a sequence, replacing each element of one value with another * value. * @param first An input iterator. * @param last An input iterator. * @param result An output iterator. * @param old_value The value to be replaced. * @param new_value The replacement value. * @return The end of the output sequence, @p result+(last-first). * * Copies each element in the input range @p [first,last) to the * output range @p [result,result+(last-first)) replacing elements * equal to @p old_value with @p new_value. */ template<typename _InputIterator, typename _OutputIterator, typename _Tp> _OutputIterator replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __old_value, const _Tp& __new_value) { // concept requirements __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator>::value_type>) __glibcxx_function_requires(_EqualOpConcept< typename iterator_traits<_InputIterator>::value_type, _Tp>) __glibcxx_requires_valid_range(__first, __last); for ( ; __first != __last; ++__first, ++__result) if (*__first == __old_value) *__result = __new_value; else *__result = *__first; return __result; } /** * @brief Copy a sequence, replacing each value for which a predicate * returns true with another value. * @param first An input iterator. * @param last An input iterator. * @param result An output iterator. * @param pred A predicate. * @param new_value The replacement value. * @return The end of the output sequence, @p result+(last-first). * * Copies each element in the range @p [first,last) to the range * @p [result,result+(last-first)) replacing elements for which * @p pred returns true with @p new_value. */ template<typename _InputIterator, typename _OutputIterator, typename _Predicate, typename _Tp> _OutputIterator replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred, const _Tp& __new_value) { // concept requirements __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator>::value_type>) __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, typename iterator_traits<_InputIterator>::value_type>) __glibcxx_requires_valid_range(__first, __last); for ( ; __first != __last; ++__first, ++__result) if (__pred(*__first)) *__result = __new_value; else *__result = *__first; return __result; } /** * @brief Assign the result of a function object to each value in a * sequence. * @param first A forward iterator. * @param last A forward iterator. * @param gen A function object taking no arguments. * @return generate() returns no value. * * Performs the assignment @c *i = @p gen() for each @c i in the range * @p [first,last). */ template<typename _ForwardIterator, typename _Generator> void generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen) { // concept requirements __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) __glibcxx_function_requires(_GeneratorConcept<_Generator, typename iterator_traits<_ForwardIterator>::value_type>) __glibcxx_requires_valid_range(__first, __last); for ( ; __first != __last; ++__first) *__first = __gen(); } /** * @brief Assign the result of a function object to each value in a * sequence. * @param first A forward iterator. * @param n The length of the sequence. * @param gen A function object taking no arguments. * @return The end of the sequence, @p first+n * * Performs the assignment @c *i = @p gen() for each @c i in the range * @p [first,first+n). */ template<typename _OutputIterator, typename _Size, typename _Generator> _OutputIterator generate_n(_OutputIterator __first, _Size __n, _Generator __gen) { // concept requirements __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, // "the type returned by a _Generator" __typeof__(__gen())>) for ( ; __n > 0; --__n, ++__first) *__first = __gen(); return __first; } /** * @brief Copy a sequence, removing elements of a given value. * @param first An input iterator. * @param last An input iterator. * @param result An output iterator. * @param value The value to be removed. * @return An iterator designating the end of the resulting sequence. * * Copies each element in the range @p [first,last) not equal to @p value * to the range beginning at @p result. * remove_copy() is stable, so the relative order of elements that are * copied is unchanged. */ template<typename _InputIterator, typename _OutputIterator, typename _Tp> _OutputIterator remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value) { // concept requirements __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -