fuse tee
fuse tee

Reputation: 69

Std Container of varying number of elements but no dynamic memory allocation

Not quite the same as Stack-Overflow Question: Constant Size Vector.

I need a container for size_t with the following properties:

  1. push_back, contains, find, and erase; as for vector<size_t>
  2. prescribed fixed memory footprint (I know the maximum number of elements at compile time as a constexpr).

The array class doesn't do the trick because array always contains the given number of elements. So I need a container that distinguishes between the memory it reserves and the items that it actually contains yet.

Who knows a suitable container in the c++2020 standard?

Upvotes: 2

Views: 142

Answers (1)

Jeffrey
Jeffrey

Reputation: 11410

The definition of vector is:

template<class T, class Allocator = std::allocator<T>> class vector;

https://en.cppreference.com/w/cpp/container/vector

Notice the Allocator part. :-)

So, if you were, say, in a high-code-maturity environment with a linter that forbids the use of new, you could provide an allocator that allocates from a fixed static pool. This allocator could enforce the needed constraints.

Upvotes: 2

Related Questions