c++ - 如何调整作为结构或类成员的 vector 的大小?

标签 c++ class vector struct

我已经编写了一个结构和类,我想知道如何使用从文本文件中读取的值来调整它们的大小。这是结构和类:

typedef struct Chrom // creating the chrom structure
{
  vector<vector <short int> > bit;
  vector<vector <short int> > WaitingTime; //this is wij in the model
  vector<vector <short int> > WaitingJob;//this is wj in the model, sigma wij must be equal to wj for each job J.
  vector<vector <short int> > StartTime;

  short int FinishTime;// finish time of each job in each machine
  int fit;
} chrom;    

在程序中我读取了一个数字,它是 m。 但是在程序中,当我尝试使用 chrom.ShortTime.resize(m) 时。它给了我一个错误。甚至我也试过这样写一个类:

class ProblemConstraint{ 
  short int Jobs, Machines;
public:
  vector <short int> Processing;
  vector <short int> t1;
  vector <short int> t2;
  short int M;
  short int W;
  void set_values(int, int);
  void resize(){
    ProblemConstraint.Processing.resize(Machines);
  }
}ProblemConstraint;

但我无法调整类中 vector 的大小。关于如何在程序的类或结构中调整 vector 大小的任何建议?

最佳答案

您的问题至少部分在于您如何调用它。

void resize(){
    ProblemConstraint.Processing.resize(Machines);
}

应该是这样的:

void resize(){
    Processing.resize(Machines);
    // OR
    this->Processing.resize(Machines);
}

任何地方都没有名为“ProblemConstraint”的对象,这是一种类型,因此您不能在其上使用 .

关于c++ - 如何调整作为结构或类成员的 vector 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45175820/

相关文章:

c++ - 如何在 Eclipse CDT 中获取 C/C++ 程序的控制台输入

c++ - 如何定义一个可以保存 uintptr_t 或 uint32_t 而无需 union 的类型?

c++ - gcc-4.8 中的这段代码发生了什么?

python - cython cdef C 类方法 : how to call it from another cython cdef class without python overhead?

r - 查找向量中下一个更高值之前的值的数量

c++ - C++ 编译器如何优化堆栈分配?

c++ - 按名称或索引引用成员变量

java - 通过 EventObject 的 Getter 和 Setter - ClassCastException 同一类 (Java)

c++ - 如何在结构或类的 vector 中快速搜索具有特定值的对象? C++

c++ - 初始化结构中结构的 vector