Google Or-Tools Glop:如何创建指向 const 对象的指针数组?
google or-tools glop: how to create array of pointer to const object?
我正在尝试使用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
任何内容。
相关文章:
- 被解释为低级别const的const对象的地址
- 在 C++ 中声明 const 对象需要用户定义的默认构造函数.如果我有一个可变成员变量,为什么不呢?
- Google Or-Tools Glop:如何创建指向 const 对象的指针数组?
- 我们可以有一个 setter 成员函数作为从 const 对象引用的 const 吗?
- "this"指向的对象是否与 const 对象相同?
- 从 const 对象访问非 const 方法
- 将非 const 对象用于 const 参数
- 如何键入用于const对象的自定义io操纵器
- 哪个 const 与哪个引用将更改参数并创建一个 const 对象?
- 无法在返回中转换 const 对象
- 接受对 const 对象的引用但没有 r 值的函数
- 当我调用 main 中使用 const 对象的 const 函数时,不断出现错误
- 在 const 对象上调用非 const 函数
- 正确的方法通过巨大的const对象的向量
- 如何重载 const 对象的 [],以便值赋值不会导致编译错误
- 相互引用的 Const 对象
- 析构函数是否可以在 const 对象上调用非 const 函数
- decltype 不会推断 const 对象的 const 成员
- 防止const函数被调用非const对象
- constexpr引用非const对象