有没有一个库可以让我在支持入队和出队的缓冲区中存储通用基类型的对象?这就是我真正需要的,我不需要对每个元素进行O(1)访问。允许每个对象具有不同的大小(多态环形缓冲区)。我可以使用基类型的指针的vector,但在我看来这有点过头了。如果我使用这种环形缓冲区,我将不必为每个对象分配单独的内存。
发布于 2017-12-08 01:46:06
正如@JTejedor提到的,boost poly collection相当直接地解决了您的问题。
如果由于某些原因您不喜欢boost,可以使用std::variant编写一个简单的解决方案
template<typename Base, typename... Derived>
struct poly
{
std::variant<Base, Derived...> var;
template<typename... Args>
poly(Args&&... args) : var{std::forward<Args>(args)...} {}
auto& get()
{
auto id = [](auto& v) -> Base& { return v; };
return std::visit(id, var);
}
operator Base&() { return get(); }
};并将其用作
struct B {};
struct D1 : B {};
struct D2 : B { int i; };
void foo()
{
using poly_t = poly<B, D1, D2>;
std::vector<poly_t> vec;
vec.push_back(B{});
vec.push_back(D1{});
vec.push_back(D2{});
B& ref = vec.back();
}发布于 2017-12-08 01:56:45
您可以使用Boost.Circular Buffer。在示例中,您可以存储指针YourInterfaceClass*或std::shared_ptr< YourInterfaceClass >,而不是int。
// Create a circular buffer with a capacity for 3 integers.
boost::circular_buffer<int> cb(3);
// Insert threee elements into the buffer.
cb.push_back(1);
cb.push_back(2);
cb.push_back(3);
int a = cb[0]; // a == 1
int b = cb[1]; // b == 2
int c = cb[2]; // c == 3
// The buffer is full now, so pushing subsequent
// elements will overwrite the front-most elements.
cb.push_back(4); // Overwrite 1 with 4.
cb.push_back(5); // Overwrite 2 with 5.
// The buffer now contains 3, 4 and 5.
a = cb[0]; // a == 3
b = cb[1]; // b == 4
c = cb[2]; // c == 5
// Elements can be popped from either the front or the back.
cb.pop_back(); // 5 is removed.
cb.pop_front(); // 3 is removed.
// Leaving only one element with value = 4.
int d = cb[0]; // d == 4我已经在我的项目中使用了它,并且没有任何问题。
https://stackoverflow.com/questions/47693119
复制相似问题