map迭代器不可递增

时间:2013-04-02 13:14:24

标签: c++ map iterator

当我尝试运行我的程序时,我得到'map / set iterator not incrementable'错误。我读到当你使迭代器失效时会发生这种情况,但我只编辑迭代器指向的对象。这是正常的行为吗?我能做些什么呢? 如果我删除此行,则没有错误:

iterator->second.neighbour[direction] = &(find_iter->second);

完整代码:

void Gomoku::addUniqueMap( const map_Map& map_to_add )
{
    this->unique_maps.push_front( map_to_add );

    std::list<map_Map>::iterator map_iter = this->unique_maps.begin();
    map_Iterator iterator = map_iter->begin();

    for( ; iterator != map_iter->end(); ++iterator )
    {
        for( int i = 1, direction = 0; i > -2; --i )
        {
            for( int j = -1; j < 2; ++j )
            {
                if( iterator->second.neighbour[direction] == nullptr )
                {
                    ++direction;
                    continue;
                }
                if( i == 0 && j == 0 )
                    continue;

                COORD pos( iterator->first.x + j, iterator->first.y + i );
                wrap( pos, this->map_size );

                map_Iterator find_iter = map_iter->find( pos );
                if( find_iter != map_iter->end() )
                {
                    iterator->second.neighbour[direction] = &(find_iter->second);
                }

                ++direction;
            }
        }
    }
}

'map_Map' - std::map<COORD, Pawn>
'map_Iterator' - std::map<COORD, Pawn>::iterator
'典当' -

struct Pawn
{
    Pawn( Player player );
    Player      player;
    const Pawn* neighbour[8];
};

1 个答案:

答案 0 :(得分:0)

错误的原因是我正在访问和修改neighbour[8],所以我正在修改out数组范围的元素,可能它已经对迭代器做了一些事情。
在Debug版本中,我遇到map/set iterator not incrementable错误,而在Release版本中,迭代器无法递增,导致程序无限循环。