C++具有任意索引的数组的类
流行的 C++ 库是否有一个类(或多个类)允许开发人员在不牺牲速度的情况下使用具有任意索引的数组?
为了使这个问题更具体,我希望能够编写类似于以下的代码:
//An array with indices in [-5,6)
ArbitraryIndicesArray<int> a = ArbitraryIndicesArray<int>(-5,6);
for(int index = -5;index < 6;++index)
{
a[index] = index;
}
Do any of the popular C++ libraries have a class (or classes) that allow the developer to use arrays with arbitrary indices without sacrificing speed ?
To give this question more concrete form, I would like the possibility to write code similar to the below:
//An array with indices in [-5,6)
ArbitraryIndicesArray<int> a = ArbitraryIndicesArray<int>(-5,6);
for(int index = -5;index < 6;++index)
{
a[index] = index;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
实际上你应该使用带有偏移量的向量。或者甚至是带有偏移量的数组。额外的加法或减法不会对程序的执行速度产生任何影响。
如果您想要与默认 C 数组具有完全相同的速度,您可以将偏移量应用于数组指针:
但是,不建议这样做。如果你真的想这样做,你应该创建一个带有内联函数的包装类来隐藏可怕的代码。您可以保持 C 代码的速度,但最终能够添加更多错误检查。
正如评论中提到的,更改数组指针后,您无法使用该指针进行删除。您必须将其重置为数组的实际开始位置。另一种方法是始终将指针保持在开头,但使用另一个修改过的指针。
Really you should be using a vector with an offset. Or even an array with an offset. The extra addition or subtraction isn't going to make any difference to the speed of execution of the program.
If you want something with the exact same speed as a default C array, you can apply the offset to the array pointer:
However, it is not recommended. If you really want to do that you should create a wrapper class with inline functions that hides the horrible code. You maintain the speed of the C code but end up with the ability to add more error checking.
As mentioned in the comments, after altering the array pointer, you cannot then delete using that pointer. You must reset it to the actual start of the array. The alternative is you always keep the pointer to the start but work with another modified pointer.
std::vector
就可以解决这个问题。对向量中单个元素的随机访问仅为 O(1)。
如果您确实需要自定义索引,您可以基于向量创建自己的小类来应用偏移。
A
std::vector<int>
would do the trick here.Random acess to a single element in a vector is only O(1).
If you really need the custom indices you can make your own small class based on a vector to apply an ofset.
使用 STL 中的映射类:
映射在内部实现为排序容器,它使用二分搜索来定位项目。
Use the map class from the STL:
map is implemented internally as a sorted container, which uses a binary search to locate items.
[这是一个旧线程,但仅供参考...]
Boost。 MultiArray 有一个范围系统,用于设置任何索引范围。
ObjexxFCL 库中的数组完全支持任意索引范围。
这些都是多维数组库。对于 OP 1D 数组需要上面的 std::vector 包装器就足够了。
[This is an old thread but for reference sake...]
Boost.MultiArray has an extents system for setting any index range.
The arrays in the ObjexxFCL library have full support for arbitrary index ranges.
These are both multi-dimensional array libraries. For the OP 1D array needs the std::vector wrapper above should suffice.
答案已编辑,因为我不太聪明。
将 std::vector 和偏移量包装到类中并提供运算符[] :
不确定这是否可以编译,但您明白了。
但不要从
std::vector
派生,请参阅注释。Answer edited because I'm not very smart.
Wrap an
std::vector
and an offset into a class and provide anoperator[]
:Not sure if this compiles, but you get the idea.
Do NOT derive from
std::vector
though, see comments.