Comparing Iterators

Mar 27, 2012 at 4:14am
Hey there folks..

I'm trying to write a search function that returns a typename iterator as shown below.

typename list<T>::iterator Search(list<T> words, typename list<T>::iterator pos, T finder)

The function dereferences the iterator passed into it and compares its value to finder. If it is not found, an else statement recursively returns Search(words, ++pos, finder), moreover advances the iterator position and starts the process over.

Where I'm running into trouble is when the iterator moves past the end of the word list and then attempts to dereference the new position.

I tried to use an if statement comparing the iterator to the end iterator of the word list as in, pos == words.end(), but it seems as this comparison never returns 1 and the "Out of Bounds Error" is never thrown.

If you have any insight as to why this might be happening, any help would be greatly appreciated.
Mar 27, 2012 at 4:37am
You are passing a copy of your list. So the 'pos' iterator is invalid as it refers to another list.
Topic archived. No new replies allowed.