【发布时间】:2019-12-13 16:35:04
【问题描述】:
为什么找不到圈时UpdateLapMap不插入UapMap?
typedef std::map<int, int> UapMap; // map of uap counters
typedef std::map<int, UapMap> LapMap; // map of UapMaps
LapMap m_LapMap;
void MyClass::UpdateLapMap( int lap, int * uaps, size_t n_uaps )
{
std::map<int, UapMap>::iterator itLap = m_LapMap.find( lap );
if ( itLap == m_LapMap.end( ) )
{
printf( "not found - insert new lap %d\n", lap );
for ( size_t i = 0; i < n_uaps; i++ ) itLap->second[ uaps[ i ] ] = 1; // initial count
}
else
{
/// insert and/or increment uap counters
}
}
【问题讨论】:
-
修改
m_LapMap.end( )的内容看起来很奇怪。 -
结束迭代器在最后一个对象之后。当
itLap == m_LapMap.end( )itLap指向一个有效对象时? -
休息一下,想想你想要达到的目标,不是这样。
-
是的。我明白为什么它不工作了。尝试将新的 UapMap 插入 LapMap。需要创建UapMap,然后插入,而不是在map.end
标签: c++ dictionary insert iterator