gpt4 book ai didi

c++ - 通过迭代器为映射成员赋值

转载 作者:行者123 更新时间:2023-11-30 04:07:51 25 4
gpt4 key购买 nike

我正在尝试遍历 map 并为其中的元素分配一个值:

typedef map<int,Node, less<int> >::const_iterator TopologyIter;

bool
RoutingManager::ActivateNewNode()
{
TopologyIter iter;
do
{
if(!iter->second.online)
{
iter->second.online = true;
iter->second.connection = myConnection->newConnections.back();
myConnection->newConnections.pop_back();
return true; //all good
}

iter++;

}while (iter != topology.end());

return false; //received a connection, but no more nodes to hand out
}

但是,我收到以下错误:

manager.cpp:91:24: error: assignment of member ‘Node::online’ in read-only object iter->second.online = true;

这是我的节点结构:

#pragma once

#include <map>
#include "nodecon.h"
typedef map<int,int, less<int> >::const_iterator NodeNeighborsIter;

struct Node
{
int id;
std::map<int,int> neighbors;
bool online;

struct NodeConnection connection;
};

我在这里错过了什么?

最佳答案

const_iterator 不能用于修改容器的内容。通过取消引用 const_iterator 获得的所有值都是 const

关于c++ - 通过迭代器为映射成员赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22188047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com