tags:

views:

64

answers:

2

Is there any other way to determine size of the container than :

//those are valid iterators from a container
BidIt begin;
BidIt end;
std::size_t size = 0;

while (begin != end)
{//Here throug iterating I'm getting adventually the correct size
   ++size;
   ++begin;
}

but I wonder if I could check size of this container by for example substracting addresses of this iterators or something like this.
Thanks for any help.

+5  A: 

You can use the distance function. Note that if your iterators are not RandomAccessIterators the distance function will use basically the same method of calculating the distance that you've shown.

Bill the Lizard
BUt if they are random access then ....
Martin York
@Martin: Then it just subtracts them. I only added that last bit to let @A-ha know that the looping method isn't so bad.
Bill the Lizard
+1  A: 

Alternatively, You can add an extra member 'size' to the BidIt type and update it whenever a insert or delete operation is performed on the container and also a getter method to get the size.

Prabhu Jayaraman