Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way of determining the size of the container

Tags:

c++

iterator

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.

like image 492
There is nothing we can do Avatar asked Oct 14 '25 14:10

There is nothing we can do


1 Answers

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.

like image 171
Bill the Lizard Avatar answered Oct 17 '25 03:10

Bill the Lizard