Spec-Zone.ru › C++

std::hive

Defined in header <hive>
template<
    class T,
    class Allocator = std::allocator<T>
> class hive;
(1) (since C++26)
namespace pmr {
    template<class T>
    using hive = std::hive<T, std::pmr::polymorphic_allocator<T>>;
}
(2) (since C++26)
1) std::hive is a sequence container that provides constant-time insertion and erasure operations.
2) std::pmr::hive is an alias template that uses a polymorphic allocator.

The hive automatically manages its storage in multiple memory blocks (element blocks). Insertion position is unspecified, so the container can reuse the memory locations of erased elements.

When active blocks (element blocks that contain elements) become empty, they can be either deallocated or become reserved blocks.

Reserved blocks become active blocks when they are reused to store elements. A user can create additional reserved blocks by calling reserve().

Erasure and iteration both have constant‑time complexity.

The capacity of each active block grows by an implementation‑defined factor (e.g., by 1.69). Both users and implementations can limit the minimum and maximum element capacities of element blocks.

A std::hive conforms to the requirements of Container, with the exception of operators == and !=. A std::hive also meets the requirements of ReversibleContainer, of AllocatorAwareContainer, and some of the requirements of SequenceContainer.

Template parameters

T - The type of the elements.
The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type is a complete type and meets the requirements of Erasable, but many member functions impose stricter requirements.
Allocator - An allocator that is used to acquire/release memory and to construct/destroy the elements in that memory. The type must meet the requirements of Allocator. The program is ill-formed if Allocator::value_type is not the same as T.

Member types

Member type Definition
value_type T
allocator_type Allocator
pointer std::allocator_traits<Allocator>::pointer
const_pointer std::allocator_traits<Allocator>::const_pointer
reference value_type&
const_reference constvalue_type&
size_type implementation-defined
difference_type implementation-defined
iterator implementation-defined LegacyBidirectionalIterator to value_type
const_iterator implementation-defined LegacyBidirectionalIterator to constvalue_type
reverse_iterator std::reverse_iterator<iterator>
const_reverse_iterator std::reverse_iterator<const_iterator>

Data members

Member Description
hive_limits current-limits (private) current block capacity limits
(exposition-only member object*)

Member functions

(constructor)
constructs the hive
(public member function)
(destructor)
destructs the hive
(public member function)
operator=
assigns values to the container
(public member function)
assign
assigns values to the container
(public member function)
assign_range
assigns a range of values to the container
(public member function)
get_allocator
returns the associated allocator
(public member function)
Iterators
begincbegin
returns an iterator to the beginning
(public member function)
endcend
returns an iterator to the end
(public member function)
rbegincrbegin
returns a reverse iterator to the beginning
(public member function)
rendcrend
returns a reverse iterator to the end
(public member function)
Capacity
empty
checks whether the container is empty
(public member function)
size
returns the number of elements
(public member function)
max_size
returns the maximum possible number of elements
(public member function)
capacity
returns the number of elements that can be held in currently allocated storage
(public member function)
reserve
reserves storage
(public member function)
shrink_to_fit
reduces memory usage by freeing unused memory
(public member function)
trim_capacity
deallocates reserved blocks and reduces capacity accordingly
(public member function)
block_capacity_limits
returns current block capacity limits
(public member function)
block_capacity_default_limits
[static]
returns block capacity default limits
(public static member function)
block_capacity_hard_limits
[static]
returns block capacity hard limits
(public static member function)
is_within_hard_limits
[static]
checks if given capacity hard limits are met
(public static member function)
reshape
reallocates active blocks so they are within block limits
(public member function)
Modifiers
emplaceemplace_hint
constructs element in-place
(public member function)
insert
inserts elements
(public member function)
insert_range
inserts a range of elements
(public member function)
erase
erases elements
(public member function)
swap
swaps the contents
(public member function)
clear
clears the contents
(public member function)
Operations
splice
moves elements from another hive
(public member function)
unique
removes consecutive duplicate elements
(public member function)
sort
sorts the elements
(public member function)
get_iterator
returns an iterator pointing to the same element as given pointer
(public member function)

Non-member functions

std::swap(std::hive)
(C++26)
specializes the std::swap algorithm
(function template)
erase(std::hive)erase_if(std::hive)
(C++26)
erases all elements satisfying specific criteria
(function template)

Deduction guides

(since C++26)

Helper classes

hive_limits
(C++26)
layout information about block capacity limits in std::hive
(class)

Notes

Feature-test macro Value Std Feature
__cpp_lib_hive 202502L (C++26) std::hive

Example

#include <chrono>
#include <hive>
#include <iomanip>
#include <iostream>
#include <list>

int main()
{
    constexpr auto iterations{10'000'000};
    auto benchmark = [=](auto& container, auto rem)
    {
        const auto t0{std::chrono::system_clock::now()};
        auto it{container.cbegin()};
        for (auto n{0}; n != iterations; ++n)
            container.insert(it, n);
        while (container.size())
            container.erase(container.begin());
        const std::chrono::duration<double> diff{std::chrono::system_clock::now() - t0};
        std::cout << std::fixed << std::setprecision(3) << diff << rem << std::endl;
    };

    std::list<std::size_t> list;
    benchmark(list, " (cold list)");
    benchmark(list, " (warm list)");

    std::hive<std::size_t> hive;
    benchmark(hive, " (cold hive)");
    benchmark(hive, " (warm hive)");
}

Possible output:

0.684s (cold list)
0.481s (warm list)
0.302s (cold hive)
0.176s (warm hive)

See also

list
doubly-linked list
(class template)

External links

1. plf::hive — A reference implementation on github.com
2. Bee hive on Wikipedia.com

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/container/hive

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API