【发布时间】:2014-01-23 02:48:39
【问题描述】:
#include <map>
struct X {
int x;
bool operator < (const X v) const
{
return (x < v.x);
}
};
struct Y {
int y;
};
int main()
{
X x = {1};
Y y = {2};
std::map <X, Y> Z;
std::pair<std::map<X, Y>::iterator,bool> lastval;
// Insert a value
lastval = Z.insert(std::pair<X, Y>(x, y));
// Erase the "last" inserted item
Z.erase(lastval.first->first);
// Error: Check if last item was erased or if iterator is valid
if (lastval.first != Z.end())
{
/* ... */
}
}
在检查最后插入的项目是否已删除时出现错误。有什么方法可以检查吗?
【问题讨论】: