Cplex C++ 音乐会技术中的多维数组

multi dimensional array in cplex c++ concert tech

本文关键字:数组 C++ 音乐会 技术 Cplex      更新时间:2023-10-16

我正在尝试在c ++音乐会技术中创建一个布尔矩阵。我已经定义了 typedef IloArray<IloBoolArray> BoolMatrix;之后我宣布:BoolMatrix Assigned(env); 但是在尝试按 file >> Assigned 输入数据时,它显示错误(没有运算符">>"匹配这些操作数)。请帮助我摆脱这个错误。谢谢

大多数情况下,这意味着您忘记在类中使用传递参数类型声明运算符">>",或者您正在使用的库类不支持这些参数的此类操作。你能更具体地回答你的问题吗?

错误告诉您的是,编译器无法找到任何带有您为其提供的参数的operator>>(Type1 LHS, Type2 RHS)定义。 Type1可能是一个std::istreamType2BoolMatrix

为了允许你尝试做的事情,你需要定义你自己的operator<<实现,它接受你给它的类型,并对这些类型进行操作。函数原型可能如下所示:

void operator<<(std::istream& file, BoolMatrix& matrix) {
  // Read in the values from the stream and add them to matrix
}

此外,从 CPLEX 文档中,它说:

模板运算符>>可以从格式为 [x, y, z, ...] 的文件中读取数值,其中 x、y、z 是类 X 的运算符>>的结果。类 X 必须提供默认构造函数,运算符>>才能工作。也就是说,语句 X x;必须为 X 工作。此输入运算符仅限于数值。

你有两个IloArrayX数组,其中 X 是外部数组的布尔数组,bool 是内部数组的布尔数组。您可以使用流从文件中读取布尔值,将它们添加到内部布尔数组 (IloBoolArray ),然后将内部IloBoolArray添加到IloArray 中。

此外,从我在网上找到的一些代码示例中,您似乎需要初始化每个IloBoolArray,然后再添加到它。

以下是我理解的将起作用的示例(这是未经测试的):

typedef IloArray<IloBoolArray>> BoolMatrix;
static constexpr int totalRows = 8;                // Total IloBoolArrays
static constexpr int rowElements = 4;              // Total elements in each bool array
int main() {
  // Create the bool Matrix
  BoolMatrix Assigned(env); 
  bool streamInputVar;                             // Temp variable to read from stream
  const char* inputFilename = "input.txt";         // File to get data from
  std::ifstream inputFile(inputFileName, ios::in); // Try and open file
  if (!inputFile) 
    // Error for failing to open file
  // File open, so read stream into Matrix
  for (int i = 0; i < totalRows; ++i) {
    Assigned.add(IlBoolArray(env));         // Add an IloBoolArray to BoolMatrix
    for (int j = 0; j < rowElements; ++j) {
      if (!(inputFile >> streamInputVar))   // Check that the stream is okay
        // Error for bad stream 
      else 
        Assigned[i].add(streamInputVar);
    }
  }
}