YouTip LogoYouTip

Cpp Libs Vector Capacity

[![Image 1: C++ Container Class ](#) C++ Container Class ](#) * * * `capacity` is a function in vector used to **query capacity**, returning the size of the currently allocated storage space in the container. `capacity` is a member function of the container class, used to **return the number of elements that the currently allocated storage space of the container can hold**. This value is usually greater than or equal to `size()`. `capacity` reflects the pre-allocated memory space inside the vector. Understanding it helps optimize program performance and avoid unnecessary memory reallocation. **Word Meaning**: `capacity` means "capacity", i.e., the number of elements the container can hold. * * * ## Basic Syntax and Parameters `capacity` is a member function of the container class, and calling it requires no parameters. ### Syntax Format size_type capacity() const; ### Parameter Description * **Parameters**: No parameters * `capacity` does not accept any parameters. ### Function Description * **Return Value**: Returns `size_type` type (usually `size_t`), representing the number of elements that the currently allocated storage space of the container can hold. * **Effect**: Returns a non-negative integer representing the allocated memory space of the container. * **Note**: `capacity()` returns the capacity, not the number of elements. Use `size()` to get the number of elements. * * * ## Examples Let's thoroughly master the usage of `capacity` through a series of examples. ### Example 1: Basic Usage - Viewing Capacity ## Example #include #include int main(){ // Empty vector std::vector emptyVec; std::cout<<"Empty vector - size: "<< emptyVec.size() <<", capacity: "<< emptyVec.capacity()<< std::endl; // Vector with elements std::vector numbers ={1, 2, 3, 4, 5}; std::cout<<"5-element vector - size: "<< numbers.size() <<", capacity: "<< numbers.capacity()<< std::endl; return 0; } **Expected Output:** Empty vector - size: 0, capacity: 05-element vector - size: 5, capacity: 5 **Code Analysis:** 1. The `capacity()` of an empty vector is 0. 2. For a vector with 5 elements, `capacity()` is at least 5 (may be larger, depending on implementation). ### Example 2: Relationship Between size and capacity Understanding the difference between `size` and `capacity` is fundamental to optimizing vector usage. ## Example #include #include int main(){ std::vector v; // Gradually add elements and observe capacity changes std::cout<<"Changes in size and capacity during element addition:"<< std::endl; for(int i =0; i <15;++i){ v.push_back(i); std::cout<<"After adding "<<(i +1)<<" elements - size: " << v.size()<<", capacity: "<< v.capacity()<< std::endl; } return 0; } **Expected Output (typical):** Changes in size and capacity during element addition:After adding 1 element - size: 1, capacity: 1After adding 2 elements - size: 2, capacity: 2After adding 3 elements - size: 3, capacity: 4After adding 4 elements - size: 4, capacity: 4After adding 5 elements - size: 5, capacity: 8After adding 6 elements - size: 6, capacity: 8After adding 7 elements - size: 7, capacity: 8After adding 8 elements - size: 8, capacity: 8After adding 9 elements - size: 9, capacity: 16After adding 10 elements - size: 10, capacity: 16After adding 11 elements - size: 11, capacity: 16After adding 12 elements - size: 12, capacity: 16After adding 13 elements - size: 13, capacity: 16After adding 14 elements - size: 14, capacity: 16After adding 15 elements - size: 15, capacity: 16 **Code Analysis:** * The capacity of vector grows exponentially (usually 2x). * When capacity is insufficient, vector will allocate a larger memory space (usually 2x the current capacity). * This strategy is to reduce the number of memory reallocations and improve performance. ### Example 3: Using reserve to Pre-allocate Capacity Using `reserve` can pre-allocate sufficient capacity to avoid frequent memory reallocations. ## Example #include #include int main(){ // Pre-allocate capacity for 1000 elements std::vector v; v.reserve(1000); std::cout<<"After reserve(1000) - size: "<< v.size() <<", capacity: "<< v.capacity()<< std::endl; // Add 100 elements for(int i =0; i <100;++i){ v.push_back(i); } std::cout<<"After adding 100 elements - size: "<< v.size() <<", capacity: "<< v.capacity()<< std::endl; return 0; } **Expected Output:** After reserve(1000) - size: 0, capacity: 1000After adding 100 elements - size: 100, capacity: 1000 **Code Analysis:** * `reserve(1000)` pre-allocates memory space that can hold 1000 elements. * After adding 100 elements, `capacity` is still 1000, with no memory reallocation occurring. * This can significantly improve performance, especially when a large number of elements need to be added. ### Example 4: shrink_to_fit to Release Excess Capacity C++11 introduced the `shrink_to_fit` method, which can adjust capacity to match size. ## Example #include #include int main(){ std::vector v; // Add 5 elements for(int i =0; i <5;++i){ v.push_back(i); } std::cout<<"Initial state - size: "<< v.size() <<", capacity: "<< v.capacity()<< std::endl; // Reserve more space v.reserve(100); std::cout<<"After reserve(100) - size: "<< v.size() <<", capacity: "<< v.capacity()<< std::endl; // Shrink to fit v.shrink_to_fit(); std::cout<<"After shrink_to_fit() - size: "<< v.size() <<", capacity: "<< v.capacity()<< std::endl; return 0; } **Expected Output:** Initial state - size: 5, capacity: 5 After reserve(100) - size: 5, capacity: 100 After shrink_to_fit() - size: 5, capacity: 5 **Code Analysis:** * `shrink_to_fit()` requests the container to reduce capacity to match size. * This can release memory that is no longer needed. * Note: This is not mandatory; the specific capacity depends on implementation. ### Example 5: Practical Application of capacity Understanding `capacity` can help optimize memory usage. ## Example #include #include void printStatus(const std::string& operation, const std::vector& v){ std::cout<< operation <<" - size: "<< v.size() <<", capacity: "<< v.capacity()<< std::endl; } int main(){ std::vector data; printStatus("Initial state", data); // If we know we need to store 1000 elements, we can pre-allocate const size_t estimatedSize =1000; data.reserve(estimatedSize); printStatus("After reserve(1000)", data); // Simulate data loading for(size_t i =0; i <1000;++i){ data.push_back(static_cast(i)); } printStatus("After adding 1000 elements", data); // Check if expansion is needed if(data.capacity()< estimatedSize){ std::cout<<"Warning: Insufficient capacity!"<< std::endl; }else{ std::cout<<"Sufficient capacity, memory allocation minimized"<< std::endl; } return 0; } **Expected Output:** Initial state - size: 0, capacity: 0 After reserve(1000) - size: 0, capacity: 1000After adding 1000 elements - size: 1000, capacity: 1000Sufficient capacity, memory allocation minimized **Code Analysis:** * Pre-estimating the required capacity and using `reserve` can avoid multiple memory allocations. * This is particularly useful for scenarios handling large amounts of data. * * C++ Container Class ](#)
← Cpp Libs Vector ResizeCpp Libs Vector Data β†’