How should a size-limited stl-like container be implemented?(应该如何实现大小受限的 stl 类容器?)
问题描述
在重构时,我想更改将条目添加到 std::vector 的数组,但为了兼容性(持久性、降级等),它仍然需要有一个上限.
拥有一个大小有限的 stl 类容器的最佳方法是什么(优雅的、类似 stl 的、有限的额外代码),因此您知道插入条目会失败?
While refactoring, I wanted to change an array where entries are added to an std::vector, but for compatibility (persistency, downgrading,...), it still needs to have an upper limit.
What is the best way (elegant, stl-like, limited extra code) to have an stl-like container which is limited in size, so you know that inserting an entry fails?
编辑:
澄清一下:我想要一个类似 stl 的容器,它开始是空的,您可以填充条目并可能删除条目并迭代填充的条目,但这不允许放入超过例如50 个条目,几乎就像一个连续的限制器,但有一个上限.
Edit:
To clarify: I would like an stl-like container, that starts empty, that you can fill with entries and possibly remove entries and that iterate over the filled-in entries, but that doesn't allow to put in more than e.g. 50 entries, so almost like a sequential contrainer, but with an upper-limit.
推荐答案
一个简单的解决方案是在您自己的有限大小的容器中封装一个向量.您可以使用私有组合或私有继承——注意私有继承模型实现并且没有公共继承的一些缺点.
A simple solution would be encapsulating a vector inside your own limited size container. You could use private composition or private inheritance --note that private inheritance models implemented in terms of and does not have some of the shortcomings of public inheritance.
编辑:具有私有继承的解决方案草图
EDIT: Sketch of the solution with private inheritance
template <typename T, unsigned int N>
class fixed_vector : std::vector<T>
{
typedef std::vector<T> vector_type;
public:
typedef typename vector_type::reference reference;
typedef typename vector_type::const_reference const_reference;
typedef typename vector_type::iterator iterator;
typedef typename vector_type::const_iterator const_iterator;
typedef typename vector_type::value_type value_type;
typedef typename vector_type::size_type size_type;
fixed_vector() : vector_type() {}
fixed_vector( size_type size, value_type const & value = value_type() )
: vector_type(size,value)
{}
void push_back( value_type v ) {
ensure_can_grow();
vector_type::push_back( v );
}
iterator insert( iterator position, value_type const & v ) {
ensure_can_grow();
vector_type::insert( position, v );
}
void reserve( size_type size ) {
if ( size > N ) throw std::invalid_argument();
vector_type::reserve( size );
}
size_type capacity() const {
// In case the default implementation acquires by default
// more than N elements, or the vector grows to a higher capacity
return std::min( vector_type::capacity(), N );
}
// provide other insert methods if required, with the same pattern
using vector_type::begin;
using vector_type::end;
using vector_type::operator[];
using vector_type::erase;
using vector_type::size;
using vector_type::empty;
private:
void ensure_can_grow() const {
// probably a different exception would make sense here:
if ( this->size() == N ) throw std::bad_alloc();
}
};
那里有相当多的挥手... std::vector
接受更多可以添加到外观的参数.如果您需要任何其他方法或 typedef,您可以使用 using
声明将它们引入范围,重新定义 typedef,或使用您的特定测试实现适配器.
There is quite a bit of hand-waving there... std::vector
take more arguments that could be added to the façade. If you need any of the other methods or typedefs, you can just bring them into scope with a using
declaration, redefine the typedef, or implement the adaptor with your particular test.
此外,在这个实现中,大小是一个编译时常量,但将其修改为构造函数参数会非常简单.
Also, in this implementation the size is a compile time constant, but it would be really simple to modify it into a constructor parameter.
这篇关于应该如何实现大小受限的 stl 类容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:应该如何实现大小受限的 stl 类容器?


基础教程推荐
- Windows Media Foundation 录制音频 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01