简体   繁体   中英

Way of determining the size of the container

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.

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.

或者,您可以向BidIt类型添加额外的成员“大小”,并在对容器执行插入或删除操作时更新它,并使用getter方法来获取大小。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM