C++具有任意索引的数组的类

发布于 2024-08-22 07:04:37 字数 298 浏览 7 评论 0原文

流行的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暖风昔人 2024-08-29 07:04:37

实际上你应该使用带有偏移量的向量。或者甚至是带有偏移量的数组。额外的加法或减法不会对程序的执行速度产生任何影响。

如果您想要与默认 C 数组具有完全相同的速度,您可以将偏移量应用于数组指针:

int* a = new int[10];
a = a + 5;
a[-1] = 1;

但是,不建议这样做。如果你真的想这样做,你应该创建一个带有内联函数的包装类来隐藏可怕的代码。您可以保持 C 代码的速度,但最终能够添加更多错误检查。

正如评论中提到的,更改数组指针后,您无法使用该指针进行删除。您必须将其重置为数组的实际开始位置。另一种方法是始终将指针保持在开头,但使用另一个修改过的指针。

//resetting the array by adding the offset (of -5)
delete [] (a - 5);

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:

int* a = new int[10];
a = a + 5;
a[-1] = 1;

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.

//resetting the array by adding the offset (of -5)
delete [] (a - 5);
╰つ倒转 2024-08-29 07:04:37

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.

哎呦我呸! 2024-08-29 07:04:37

使用 STL 中的映射类:

std::map<int, int> a;
for( int index = -5; index < 6; ++index )
{ 
    a[index] = index; 
}

映射在内部实现为排序容器,它使用二分搜索来定位项目。

Use the map class from the STL:

std::map<int, int> a;
for( int index = -5; index < 6; ++index )
{ 
    a[index] = index; 
}

map is implemented internally as a sorted container, which uses a binary search to locate items.

波浪屿的海角声 2024-08-29 07:04:37

[这是一个旧线程,但仅供参考...]

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.

孤芳又自赏 2024-08-29 07:04:37

答案已编辑,因为我不太聪明。

将 std::vector 和偏移量包装到类中并提供运算符[] :

template <class T>
class ArbVector
{
    private:
        int _offset;
        std::vector<T> container;
    public:
        ArbVector(int offset) : _offset(offset) {}
        T& operator[](int n) { return container[n + _offset] }
};

不确定这是否可以编译,但您明白了。

但不要从 std::vector 派生,请参阅注释。

Answer edited because I'm not very smart.

Wrap an std::vector and an offset into a class and provide an operator[]:

template <class T>
class ArbVector
{
    private:
        int _offset;
        std::vector<T> container;
    public:
        ArbVector(int offset) : _offset(offset) {}
        T& operator[](int n) { return container[n + _offset] }
};

Not sure if this compiles, but you get the idea.

Do NOT derive from std::vector though, see comments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文