更新标准::映射对象数据
Updating std::map object data
我有一个std::map对象如下
typedef std::map<int,int> RoutingTable;
RoutingTable rtable;
然后我在一个函数中初始化了它
for (int i=0; i<getNumNodes(); i++)
{
int gateIndex = parentModuleGate->getIndex();
int address = topo->getNode(i)->getModule()->par("address");
rtable[address] = gateIndex;
}
现在我想在另一个函数中更改 rtable 中的值。 我怎样才能做到这一点?
通过引用传递rtable
:
void some_func(std::map<int, int>& a_rtable)
{
// Either iterate over each entry in the map and
// perform some modification to its values.
for (std::map<int, int>::iterator i = a_rtable.begin();
i != a_rtable.end();
i++)
{
i->second = ...;
}
// Or just directly access the necessary values for
// modification.
a_rtable[0] = ...; // Note this would add entry for '0' if one
// did not exist so you could use
// std::map::find() (or similar) to avoid new
// entry creation.
std::map<int, int>::iterator i = a_rtable.find(5);
if (i != a_rtable.end())
{
i->second = ...;
}
}
相关文章:
- 如何在没有数据拷贝的情况下从指针创建一个Eigen VectorXd对象
- 如何使用数据对象上的常量指针初始化类
- int数据类型的指针指向的是什么,如果是一个类的私有数据成员,我们创建了该类的两个对象?
- 使用指针访问数组中的对象数据成员
- 使用对象文件读取三角形数据网格
- 访问数据成员(本身是对象)的数据成员,就好像它们是类成员一样
- 如何在新的派生对象中获取基本对象的数据?
- 如何从 CSV 获取数据并将其存储在 C++ 中的表对象中
- 类对象在 c++ 中是否具有数据类型?
- 有没有办法在C++中循环访问对象的不同数据成员
- C++ 在节点内存储对数据对象的引用,而不复制数据
- 带有 std::map 的 c++ 结构是拥有动态数据对象的好设计吗?
- 将任意的JavaScript数据对象传递到Node.js C 插件
- Vtk检查多数据对象是否相交
- 如何处理类结构的变化,并与使用 boost 序列化的数据/对象向后兼容
- 如何处理错误“vtkTrivialProducer :此数据对象不包含请求的范围”
- 如何在不复制数据和不公开指针的情况下向用户检索数据对象
- 如何赋值具有const成员的数据对象
- 对于普通数据对象,多态的正确方式是什么?
- 为什么这会创建两次数据对象