The method of determining the size of the container

Is there any other way to determine the 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 can check the size of this container, for example, by subtracting the addresses of these iterators or something like that. Thanks for any help.

+3
source share
2 answers

You can use the distance function . Note: if your iterators are not RandomAccessIterators , the distance function will use basically the same method of calculating the distance traveled.

+5
source

BidIt , , getter .

0

Source: https://habr.com/ru/post/1756343/


All Articles