#containers
3 posts tagged #containers.
You cannot move out of a priority_queue without a const_cast
priority_queue::top() returns a const reference, so a move-only element cannot be taken out of the queue at all: the copy constructor is selected and the code does not compile. The accepted workaround is a const_cast immediately followed by pop(), which is safe for one specific reason worth understanding before you write it.
std::inplace_vector is a vector that never touches the heap
C++26 adds std::inplace_vector<T, N>, a sequence container with a fixed compile-time capacity whose storage lives inside the object. It gives you a vector's dynamic size and push_back with zero heap allocation, which is exactly what embedded, real-time, and hot-path code has been hand-rolling for decades. GCC 16.1 ships it now.
std::flat_map is just two vectors
C++23's std::flat_map is an adaptor over a sorted vector of keys and a parallel vector of values. That buys cache-friendly lookups and near-zero memory overhead, and costs O(n) insertion and aggressive iterator invalidation. It replaces std::map, not std::unordered_map, and knowing which vector trick it is tells you exactly when to reach for it.