Namespaces
Variants

std::hive<T,Allocator>::capacity

来自 zh.cppreference.net

 
 
 
 
size_type capacity() const noexcept;

返回 hive 在不需要重新分配更多元素块的情况下可以容纳的元素数量。

返回值

当前已分配存储的容量。

复杂度

常数。

示例

#include <iomanip>
#include <iostream>
#include <hive>

int main()
{
    int sz = 1000;
    std::hive<int> v;

    auto cap = v.capacity();
    std::cout << "Initial size: " << v.size() << ", capacity: " << cap << '\n';

    std::cout << "\nDemonstrate the capacity's growth policy."
                 "\nSize:  Capacity:  Ratio:\n" << std::left;
    while (sz-- > 0)
    {
        v.insert(sz);
        if (cap != v.capacity())
        {
            std::cout << std::setw( 7) << v.size()
                      << std::setw(11) << v.capacity()
                      << std::setw(10) << v.capacity() / static_cast<float>(cap) << '\n';
            cap = v.capacity();
        }
    }

    std::cout << "\nFinal size: " << v.size() << ", capacity: " << v.capacity() << '\n';
}

可能的输出:

Initial size: 0, capacity: 0

Demonstrate the capacity's growth policy.
Size:  Capacity:  Ratio:
1      76         inf       
77     152        2         
153    304        2         
305    559        1.83882   
560    814        1.45617   
815    1069       1.31327   

Final size: 1000, capacity: 1069

另请参阅

返回元素数量
(公开成员函数)
预留存储空间
(公开成员函数)