C++与 Java 一样?:具有 *可变* 长度 int 的向量的向量
我的模型最好使用一些
v int[30][i][N_i];
由 30 个整数元组向量组成的结构,其中
v[0] 是一个虚拟值,
v[1] 是普通整数(其中 N_0 个),
v[2] 是 int 对(N_1 对)
...
v[29] 将是 29 个 int 元组(其中 N_29 个),
这不是 vector
就像“generic-vector-of-显然
,外部的固定dim=30是没有问题的,内部的由自扩展的STL向量类来处理。
有没有办法让中间尺寸固定,但不是恒定的?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如我在对你的问题的评论中所写的那样,我不确定你在寻找什么(顺便说一句,我对“as in Java”部分非常感兴趣)。
但因为我认为看看如何使用 Boost 生成它会很有趣。MPL(和融合...和数组< /a>),我假设您想要一个静态定义的结构,其 Ntn 元素是大小为 N 的 int 数组的向量:
As I wrote in a comment to your question, I'm not sure to understand what you are looking for (and I'm very interested by the "as in Java" part, BTW).
But since I thought it would be fun to see how it could be generated with Boost.MPL (and Fusion... and Array), I'm assuming that you want a statically defined structure whose Ntn element is a vector of int arrays of size N:
完成您想要的操作的最佳方法是围绕向量访问器编写包装函数。包装现有行为的最佳方法是编写一个新类,该类是用您喜欢的向量或向量来实现的。
The best way to do what you want is write wrapper functions around the vector accessors. The best way to wrap existing behavior is to write a new class that is implemented with your fancy vector of vector of what ever.
我是 Michael(最初作者),现在的 ID 是 La-AIDA
首先,谢谢大家,Boost & 。融合对我来说很新鲜。
致埃里克:
一个错字:v[1] 应该有 N_1 个条目,v[2] N_2 等等。
我想要类似 STL 的东西,而不是 C 数组(缺少边界检查,无法添加它)。
对埃里克的新评论:
我尝试了你的解决方案,它立即起作用了(几乎在删除虚拟查询后)!
谢谢你!
但是:我需要类似的东西
,即 At<...> 的索引。应该是可变的(这就是重点,能够运行索引而不是单独处理 30 个硬编码的东西)
但 gcc 抱怨错误: 'i' 不能出现在常量表达式中
关于“as in Java”:
AfaIk,Java 中的二维矩阵不是什么
int v[10][10];
具有固定尺寸,但类似
int[][] v;
你首先有一个
(或类似的语法),然后,这就是要点:
它形成一个三角矩阵,或者当然是你喜欢的任何形式。
事实上,常规的 10×10 矩阵也需要 1 加 10 个新矩阵。
关于结构本身:
是一个等效的数据结构
然而,我们必须分别处理这 30 个部分中的每一个部分,这
。我希望能够说
v[5][3][123] = 99;
将第 123 个 5 元组中的第三个分量设置为 99,没有定义
哪个可以解决问题,但浪费了巨大的空间,因为
v[1][2..30][0..\infty] 或更一般的 v[i][i+1..30][*]
永远不会被使用。所以,在我的问题中,我有一个 int 列表,另一个列表对,三元组,...,30 个 int 元组,所有这些都应该在单个结构内进行排序等,而不浪费空间。
I'm Michael (the initial author) now having ID La-AIDA
Firstly, thank you all, Boost & Fusion where new to me.
To Éric:
One Typo: v[1] should have N_1 entries, v[2] N_2 and so forth.
I would like STL-like stuff, not C-arrays (lacking bounds check with no option to add it).
New comment to Éric:
I tried your solution, it worked (almost, after removing the dummy query) immediately!
Thank you!
But: I need something like
that is, the index for At<..> should be variable (that is the whole point, to be able to run an index instead of treating 30 hard-coded things separately)
but gcc complains with error: 'i' cannot appear in a constant-expression
About the "as in Java":
AfaIk, a two-dim matrix in Java is not some
int v[10][10];
with fixed dimensions, but something like
int[][] v;
where you first have a
(or similar syntax) and then, and this is the point:
which makes a triangular matrix, or of course any form you like.
In fact a regular 10-by-10 matrix as well requires 1 plus 10 new's.
About the structure itself:
An equivalent data structure would be
where however we would have to address each of the 30 parts separately.
I would like to be able to say
v[5][3][123] = 99;
to set the 3rd component in the 123rd 5-tuple to 99, without defining
which would do the trick, but wasting enormous space, since
v[1][2..30][0..\infty] or more generally v[i][i+1..30][*]
are never used.So, in my problem, I have a list of int's , another of pairs, of triples,..., of 30-tuples of int's, which all should be sortable etc., within a single structure, without wasting space.