c++ - 非常量对象的 const vector

标签 c++ vector constants const-correctness

在接口(interface)中定义函数:

virtual void ModifyPreComputedCoeffs ( std::vector < IndexCoeffPair_t > & model_ ) = 0;

我们希望指定 vector 模型_不应更改,因为不应对 vector 执行 push_back 等操作,但模型_中的 IndexCoeffPair_t 结构对象可以更改。 我们应该如何指定?

virtual void ModifyPreComputedCoeffs ( const std::vector < IndexCoeffPair_t > & model_ ) = 0;

我认为不起作用。

最佳答案

与其将 vector 传递给函数,不如按照标准库的方式传递一对迭代器。

virtual void ModifyPreComputedCoeffs ( std::vector < IndexCoeffPair_t >::iterator & model_begin, std::vector < IndexCoeffPair_t >::iterator & model_end )

关于c++ - 非常量对象的 const vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6819699/

相关文章:

c++ - 如何在 Xcode 中获得 std::optional 支持?

java - 如何将Java代码转换成Matlab

c++ - vector 正确吗?

c# - 在 C# 中声明 const 的更优雅的方法是什么

C++ 映射 - 表达式必须是整型常量表达式

c++ - 具有多种类型的类数组?如何访问数组中的一种类型?

c++ - 我需要在 C++ 中销毁一个字符串吗

c++ - 为特定 ID 搜索结构 vector 的有效方法?

java - 无法在内部类中声明 Public static final String s = new String ("123")

swift - 在运行时创建常量,但在其他函数中访问常量