[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: STL iterators
From: |
Andre Poenitz |
Subject: |
Re: STL iterators |
Date: |
Tue, 25 Apr 2006 22:13:32 +0200 |
Ulrich Eckhardt <doomster@knuut.de> wrote:
> Gelu Ionescu wrote:
>> My hope is to detect a substring in a string using 2 string::iterators
>> it1 & it2.
>
> Don't, std::string has other methods for that. Otherwise, you would need an
> algorithm that searches one sequence within another sequence, which is
> easy to write but AFAIK not part of the C++ standardlibrary
Shouldn't the following do the trick?
/**
* @brief Search a sequence for a matching sub-sequence.
* @param first1 A forward iterator.
* @param last1 A forward iterator.
* @param first2 A forward iterator.
* @param last2 A forward iterator.
* @return The first iterator @c i in the range
* @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
* for each @c N in the range @p [0,last2-first2), or @p last1 if no
* such iterator exists.
*
* Searches the range @p [first1,last1) for a sub-sequence that compares
* equal value-by-value with the sequence given by @p [first2,last2) and
* returns an iterator to the first element of the sub-sequence, or
* @p last1 if the sub-sequence is not found.
*
* Because the sub-sequence must lie completely within the range
* @p [first1,last1) it must start at a position less than
* @p last1-(last2-first2) where @p last2-first2 is the length of the
* sub-sequence.
* This means that the returned iterator @c i will be in the range
* @p [first1,last1-(last2-first2))
*/
template<typename _ForwardIter1, typename _ForwardIter2>
_ForwardIter1
search(_ForwardIter1 __first1, _ForwardIter1 __last1,
_ForwardIter2 __first2, _ForwardIter2 __last2)
> All this has nothing to do with the GCC's C++ compiler though.
Example was taken from a gcc installation ;-}
Andre'