简体   繁体   English

如何在 C++ 的另一个二维数组中使用一个数组,如大小或维度

[英]how to use an array like a size or dimension in another array of 2D in C++

i have a set of Jobs J= 10, and h is the index of J, Each job h is executed exactly in one mode m in M(h) where m stands for the number of workers assigned to the job h and is related to a processing time Pjob(h,m). i 有一组作业 J= 10,h 是 J 的索引,每个作业 h 在 M(h) 中恰好以一种模式 m 执行,其中 m 代表分配给作业 h 的工人数,并且与处理时间 Pjob(h,m)。

can anybody help me and fix it and how to use M(h) in For loop任何人都可以帮助我修复它以及如何在 For 循环中使用 M(h)

So this my code of declaration:所以这是我的声明代码:

#include <iostream> 
using namespace std;

#define J 10   
int h; // index of Job J 
int m; // index of Mode M

int M[J] = { 2, 2 , 3 , 4 , 5 , 1 , 3, 3 , 2 , 1 };// index m
int Pjob[J][M[J]];// processing time for job h performed in mode m 

so it shows me an errors and i thought that the declaration was fault:所以它向我显示了一个错误,我认为声明是错误的:

Error (active) E0028 expression must have constant value Error C2131 expression was not evaluated to constant Error C2148 total array size must not exceed 0x7ffffffff bytes

can anybody help me and fix it and how to use M(h) in For loop任何人都可以帮助我修复它以及如何在 For 循环中使用 M(h)

Arrays must have regular dimensions (meaning, they must be square, or cubic, etc). Arrays 必须具有规则尺寸(意思是,它们必须是正方形或立方体等)。 Just declare your Pjob array to have the largest regular size:只需声明您的Pjob数组具有最大的常规大小:

#define J 10
#define MAX_MJ 5
int M[J] = { 2, 2 , 3 , 4 , 5 , 1 , 3, 3 , 2 , 1 };// index m
int Pjob[J][MAX_MJ];// processing time for job h performed in mode m 

Then use it with your special indexing:然后将它与您的特殊索引一起使用:

assert(job < J);
assert(mode < M[job]);
quux( Pjob[job][mode] );

This wastes space for unused elements, but for such a small object it is unlikely to be a problem.这为未使用的元素浪费了空间,但对于这么小的 object 来说,这不太可能成为问题。

The other option is to use a std::vector or other library container.另一种选择是使用std::vector或其他库容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM