18

Is it possible to compare two iterators? A comparision using std::min

void change ( typename TList <Item *>::Type ::iterator it_begin, typename TList <Item*>::Type ::iterator it_end )
{
   ....
this->items.resize ( index );
   std::sort ( it_begin, std::min (it_end, it_begin += index - 1); //Compare two iterators, exception
....
}

throws the following exception:

Assertion failed: Vector iterators  incompatible... 

Is there any other way of the comparision?

1
  • What are the types of TList<Item *>::Type and this->items? Do the iterators point into this->items? Commented Aug 28, 2013 at 18:21

4 Answers 4

25

Yes. But I doubt if you can do that with std::min.

You can use std::distance function to calculate the distance between two iterators. And then you can use the distance to determine which iterator is the smaller one. Once you know the smaller iterator, you can pass that to std::sort function.

Here is small illustration how to calculate distance:

#include <iostream>
#include <iterator>
#include <vector>

int main() {
    std::vector<int> v(100); //vector of size 100
    std::cout <<(std::distance(v.begin(), v.begin() + 10))<< std::endl;
    std::cout <<(std::distance(v.begin() +25, v.begin() +10))<< std::endl;
}

Output:

10
-15

Hope that gives you enough idea how to proceed to do what you want to.

Sign up to request clarification or add additional context in comments.

7 Comments

"And then you can use the distance to determine which iterator is the smaller one." False. "The behavior is undefined if last is not reachable from first by (possibly repeatedly) incrementing first."
I think you will only want to do this if iterators are in fact random access iterators. Since they are the iterators that support comparison operation.
Mooing Duck: Before C++11. Since C++11 it's If InputIt is RandomAccessIterator, the behavior is undefined if last is not reachable from first and first is not reachable from last.
Can you help with what's the complexity of std::distance() function, is it not O(1) ?
@KartikRaj: there are many kinds of iterators. see this: stackoverflow.com/questions/5211914/… .. e.g the iterators that behave like pointers are random-access-iterator because you can access random elements by using offset i.e *(it + offset). You cannot do the same if it's InputIterator, or ForwardIterator.
|
6

In the book C++ Primer 5th Ed. on p.111 section 3.4.2 Iterator Arithmetic says,

we can use == and != to compare to valid iterators into any of the library containers.

The section also tells us that iterators for string and vector support relational operators (aka iterator arithmetic) which include >, >=, <, <=.

Comments

3

After calling resize, all your existing iterators are invalid.

Furthermore, that line invokes undefined behavior, since you're both changing it_begin and reading from it, in an undetermined order.

Comments

3

To answer the question, std::distance() can be used to measure the distance to the begin() iterator, and these distances can then be compared. However, as pointed out by Ben, there are other problems with your code. See http://www.cplusplus.com/reference/std/iterator/distance/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.