이 코드 덩어리에서는지도에 쌍을 추가하고 모든 것이 잘되지만지도가 마지막이 아닌 쌍을 삭제하면 괜찮습니다. 쌍을 더 추가하십시오. 내가 뭘 잘못하고있어 ??STL지도는 첫 번째 쌍을 제거한 후에 쌍을 추가하지 않습니다.
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;
}
및 reorgObjectList에 대한이 삭제 코드
typedef std::pair<int, Object> orderedObject;
typedef std::map<int, Object> ObjectList;
다음과 같이
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 선언된다 :
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;
}
제안 사항 ???
삭제를 어떻게 하시겠습니까? –
개체가 선언 된 위치는 어디입니까? – jalf
더 많은 코드를 게시해야합니다. jalf가 제안하는 것처럼, 객체가 선언 된 곳은 어디입니까? 여기에 다른 문제가있는 것처럼 보이며 사용법, 스타일 및 디자인에 대한 제안을 드릴 수 있습니다. 나는 항상지도를 사용합니다 ... 나는 결코 이런 식으로 사용하지 않는 것 같습니다. – Tom