Google Or-Tools Glop:如何创建指向 const 对象的指针数组?

google or-tools glop: how to create array of pointer to const object?

本文关键字:const 对象 数组 指针 创建 Glop Or-Tools 何创建 Google      更新时间:2023-10-16

我正在尝试使用Google Glop进行研究项目官方网站

我正在玩源代码附带的示例

MPVariable* const x = solver.MakeNumVar(0.0, 1, "x");

我可以看到如何在 glop 中创建变量。

但是,我不明白,如果在编译时没有给出变量的数量怎么办? 如果程序从网络接收命令并即时构建问题。

我想应该有一个变量数组?

std::vector<MPVariable*> variables;// is this correct? or it should be:
std::vector<MPVariable const *> variables; // never saw syntax like this before
variables.resize(100);//say, 100 variables
for(std::size_t i = 0; i < variables.size(); ++i){
variables[i] = solver.MakeNumVar(0.0, createSomeNumber(), createName());
}

这是它应该的工作方式吗? 任何人都可以在这里分享一些经验吗?

如果你真的想要指向常量变量的指针,我会选择:

std::vector<const MPVariable*> variables;

但我一直使用

std::vector<MPVariable*> variables;

请注意,您没有始终如一地放置const

`MPVariable* const x`

vs.

`std::vector<MPVariable const *>`

函数MakeNumVar()返回一个MPCariable*,因此无需const任何内容。