小编bli*_*587的帖子

Python中的"反转"比较运算符

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)

python comparison

6
推荐指数
1
解决办法
999
查看次数

使用gcc编译的C++在VC++ 14中有效且无效

如果我使用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)

c++ visual-c++

0
推荐指数
1
解决办法
63
查看次数

标签 统计

c++ ×1

comparison ×1

python ×1

visual-c++ ×1