stl集合迭代器 [英] stl set iterator

查看:207
本文介绍了stl集合迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个stl设置与Cell类的对象

I have an stl set with objects of Cell class

class Cell
{
public:
    //Ctor/Dtor
    Cell(size_t cellId=0,int x =0,int y = 0,size_t t = 0):m_cellId(cellId),m_x(x),m_y(y),m_t(t),m_color(WHITE),m_pCellId(0),m_regNum(0){}
    ////////////////////
    //    Mutators    //
    ////////////////////
    void schedNode(size_t t,int nodeId){m_sched[t] = nodeId;}
    void setColor(color c){m_color = c;}
    void setParentId(size_t pId){m_pCellId = pId;}
         //.....
}

每个 Cell m_x m_y (成员)坐标+其他数据成员( m_t,m_color,m_pCellId,m_regNum

Each Cell has m_x and m_y (member) coordinates + additional data members(m_t,m_color,m_pCellId,m_regNum)

comapareCells类仅用于根据实际m_x和m_y坐标查找单元格:

comapareCells class is used to find the cell only based on its actual m_x and m_y coordinates:

class comapareCells
{
public:
    bool operator()(const Cell& lCell,const Cell& rCell)
    {
        if(lCell.getX() < rCell.getX())
            return true;
        else if(lCell.getX() == rCell.getX())
            return (lCell.getY() < rCell.getY());
        else
            return false;
    }

};

我运行以下命令为了找到实际单元格:
c 是只需要单元格坐标的单元格。它用于查找包含在cet中的具有给定坐标的实际单元格,并尝试对实际单元格进行一些操作:

I run the following in order to find "actual cell": c is a cell which has only needed cell coordinates. It is used to find the actual cell, contained in the cet, having given coordinates and try to make some operations on the actual cell:

set<Cell,comapareCells>::iterator itCell;
    if((itCell = m_cells.find(c)) == m_cells.end())
        return;
    if(itCell->getColor() != WHITE)
        return;
    itCell->setColor(GRAY);
    itCell->setParentId(cIdFrom);

我收到的编译错误itCell-> setColor ; itCell-> setParentId(cIdFrom);


如何解决?

cannot convert 'this' pointer from 'const Cell' to 'Cell &'

推荐答案


>解决方案

解决方案

通过迭代器更改 set 的元素的值是不合法的,因为 / code>无法知道您所更改的内容,并使其失效。如果你想要改变它,你必须删除它,并重新插入与您的更改。

It is not legal to change the value of a set's elements through an iterator because the set has no way of knowing what you have changed and would invalidate it. If you want to change it like that you would have to remove it and reinsert it with your changes.

Cell newCell(*itCell);
newCell.setColor(GRAY);
m_cells.erase(itCell);
m_cells.insert(newCell);

这篇关于stl集合迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆