向量保存只读矩阵?

发布于 2024-09-06 08:55:15 字数 1519 浏览 6 评论 0原文

我想使用一个向量来保存大小为 5x5 的只读整数矩阵,

vector<const int[5][5]> startingPieces;

但是这个声明会导致一堆我以前从未见过的奇怪错误。

error C2535: 'const int (*std::allocator<_Ty>::address(const int (&)[5][5]) const)[5][5]' : member function already defined or declared
1>        with
1>        [
1>            _Ty=const int [5][5]
1>        ]
1>        c:\program files\microsoft visual studio 9.0\vc\include\xmemory(109) : see declaration of 'std::allocator<_Ty>::address'
1>        with
1>        [
1>            _Ty=const int [5][5]
1>        ]
1>        c:\program files\microsoft visual studio 9.0\vc\include\vector(429) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1>        with
1>        [
1>            _Ty=const int [5][5]
1>        ]
1>        c:\program files\microsoft visual studio 9.0\vc\include\vector(439) : see reference to class template instantiation 'std::_Vector_val<_Ty,_Alloc>' being compiled
1>        with
1>        [
1>            _Ty=const int [5][5],
1>            _Alloc=std::allocator<const int [5][5]>
1>        ]
1>        c:\users\eric\documents\visual studio 2008\projects\testing grounds\testing grounds\main.cpp(14) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1>        with
1>        [
1>            _Ty=const int [5][5]
1>        ]

那么,这个声明有什么问题呢?

I want to use a vector to hold read-only integer-matrices of the size 5x5

vector<const int[5][5]> startingPieces;

But this declaration causes a bunch of weird errors I've never ever seen before.

error C2535: 'const int (*std::allocator<_Ty>::address(const int (&)[5][5]) const)[5][5]' : member function already defined or declared
1>        with
1>        [
1>            _Ty=const int [5][5]
1>        ]
1>        c:\program files\microsoft visual studio 9.0\vc\include\xmemory(109) : see declaration of 'std::allocator<_Ty>::address'
1>        with
1>        [
1>            _Ty=const int [5][5]
1>        ]
1>        c:\program files\microsoft visual studio 9.0\vc\include\vector(429) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1>        with
1>        [
1>            _Ty=const int [5][5]
1>        ]
1>        c:\program files\microsoft visual studio 9.0\vc\include\vector(439) : see reference to class template instantiation 'std::_Vector_val<_Ty,_Alloc>' being compiled
1>        with
1>        [
1>            _Ty=const int [5][5],
1>            _Alloc=std::allocator<const int [5][5]>
1>        ]
1>        c:\users\eric\documents\visual studio 2008\projects\testing grounds\testing grounds\main.cpp(14) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1>        with
1>        [
1>            _Ty=const int [5][5]
1>        ]

So, what is wrong with this declaration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

┊风居住的梦幻卍 2024-09-13 08:55:15

有两件事 - 首先向量不能容纳 const 对象 - 请参阅 我可以在向量中使用 const 来允许添加元素,但不是对已添加的修改? 对此进行讨论。其次,它们不能保存数组,因为它们保存的内容必须是可复制和可分配的,而数组两者都不是。

Two things - firstly vectors cannot hold const objects - see Can I use const in vectors to allow adding elements, but not modifications to the already added? for a discussion of this. And secondly they cannot hold arrays, as the things they hold must be copyable and assignable, and arrays are neither.

菊凝晚露 2024-09-13 08:55:15

您在这里应该做的是创建自己的矩阵类来存储 5x5 数据数组,然后用它创建向量。

What you should do here is create your own matrix class that stores the 5x5 array of data and then create your vector with that.

笨笨の傻瓜 2024-09-13 08:55:15

一种选择是使用 array class (您的实现可能支持 std::arraystd::tr1::array;如果不支持,您可以使用 boost::array 来自 Boost 库):

std::vector<std::array<std::array<int, 5> > >

存储在容器中的元素仍然不能是 const;如果适合您的用例,您可以使整个向量为常量。

One option is to use the array class (your implementation may support std::array or std::tr1::array; if not, you can use boost::array from the Boost libraries):

std::vector<std::array<std::array<int, 5> > >

The elements stored in the container still cannot be const; you can make the entire vector const if that works for your use case.

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