在这段代码中,我在地图上添加了一对,一切都很好,但是当我删除不是最后一个的一对时,地图不再添加任何对。我做错了什么??
SomeClass::add(Object object)
if (!object.empty())
{
ObjectList::iterator result = find(object.name());
if (result == ObjectList.end())
{
object.order(size() + 1);
ObjectList.insert(orderedObject(object.order(), object));
}
else
{
ObjectList[result->first] = object;
}
}
ObjectList 和 orderedObject 声明如下:
typedef std::pair<int, Object> orderedObject;
typedef std::map<int, Object> ObjectList;
这是删除代码:
SomeClass::eraseNamed(std::string aName)
{
if (!isEmpty())
{
ObjectList::iterator result;
result = find(aName);
if (result != ObjectList.end())
{
ObjectList.erase(result);
reorgObjectList();
return true;
}
}
return false;
}
对于查找方法:
ObjectList::iterator SomeClass::find(std::string aName)
{
ObjectList::iterator result = ObjectList.begin();
while (result != ObjectList.end())
{
if (aName == result->second.name())
return result;
result++;
}
return result;
}
对于重组对象列表:
bool SomeClass::reorgObjectList()
{
ObjectList::iterator i=ObjectList.begin();
int j=1;
for (i = ObjectList.begin(); i != ObjectList.end(); ++i)
{
if(j!=i->second.order())
i->second.order(j);
j++;
}
return true;
}
有什么建议么???