class Inner():
def __init__(self, x):
self.x = x
def __eq__(self, other):
if isinstance(other, Inner):
return self.x == other.x
else:
raise TypeError("Incorrect type to compare")
class Outer():
def __init__(self, y):
self.y = Inner(y)
def __eq__(self, other):
if isinstance(other, Outer):
return self.y == other.y
elif isinstance(other, Inner):
return self.y == other
else:
raise TypeError("Incorrect type to compare")
if __name__ == "__main__":
a = Outer(1)
b = Inner(1)
print(a == b) # ok no problem
print(b == a) # This will raise a …Run Code Online (Sandbox Code Playgroud) 如果我使用gcc编译并运行以下代码,它可以正常工作 - 我不明白为什么这不应该.但是,如果我使用VC++编译并运行这段代码,它会失败并且弹出窗口显示:"Expression:Vector iterators incompatible"
int main() {
vector<int> v = { 1,2,3,4 };
for(auto it = v.begin(); it != v.end(); )
{
if(*it% 2 == 0)
{
v.erase(it);
}else
{
++it;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)