简体   繁体   中英

Why NSMutableSet can be added more objects than capacity

Create one nsmutableset with capacity for example 3, but you can add more than 3 objects into it. So what is the real capacity ?

From Apple Docs, seems like this is okay, but what is the purpose of "capacity" ?

Return Value A mutable set with initial capacity to hold numItems members.

Discussion Mutable sets allocate additional memory as needed, so numItems simply establishes the object's initial capacity.

The real capacity is only limited by the available memory. The initial capacity just allows you to avoid growing the set dynamically when you know the actual or approximate size of the set. This is more efficient than continually growing the set.

For example, if you wanted a set to store the letters in a string (ie attack = {atck}), you know there are at most 26 different letters, so you can initialise with initial capacity 26 - this is a "hint" to the initialiser. It may allocate a larger or smaller capacity.

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