标签: equality-operator

在比较值之前比较使用`==`比较身份?

如果我使用比较两个变量==,Python会比较身份,如果它们不相同,那么比较值?

例如,我有两个指向同一个字符串对象的字符串:

>>> a = 'a sequence of chars'
>>> b = a
Run Code Online (Sandbox Code Playgroud)

这是比较值,还是只比较ids?:

>>> b == a
True
Run Code Online (Sandbox Code Playgroud)

首先比较身份是有意义的,我想是这样的,但我还没有在文档中找到任何支持这一点的东西.我得到的最接近是这样:

x==y 电话 x.__eq__(y)

这并没有告诉我在打电话之前是否做了什么x.__eq__(y).

python identity equality-operator

3
推荐指数
1
解决办法
116
查看次数

gcc 和 MSVC/clang 之间使用 C++20 重载模板化相等比较运算符的不同结果

考虑以下相等运算符的实现,用 C++20 编译(在godbolt上运行):

#include <optional>

template <class T>
struct MyOptional{
    bool has_value() const { return false;}
    T const & operator*() const { return t; }
    T t{};
};

template <class T>
bool operator==(MyOptional<T> const & lhs, std::nullopt_t)
{
    return !lhs.has_value();
}

template <class U, class T>
bool operator==(U const & lhs, MyOptional<T> const & rhs)
{
    // gcc error: no match for 'operator==' (operand types are 'const std::nullopt_t' and 'const int')
    return rhs.has_value() ? lhs == *rhs : …
Run Code Online (Sandbox Code Playgroud)

c++ gcc language-lawyer equality-operator c++20

2
推荐指数
1
解决办法
89
查看次数


Java:在String和int变量上使用equals()和===

我试图理解相等(==)equals()方法,但无法推断出这种行为.有人可以使用以下println语句解释此行为.

String a="Hai";
String b="Hai";
int c=5, d=5;

System.out.println("Check1 : " + (c==d)); //prints  "Check1 : true"

System.out.println("Check2 : " + a==b); //prints false. It didn't print the string "Check2 : "

System.out.println("Check3 : " +a.equals(b)); //prints "Check3 : true"

System.out.println(" c==d : " + c==d);   //compile time error - incomparable types: java.lang.String and int
Run Code Online (Sandbox Code Playgroud)

非常感谢.

java equals equality-operator

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

无法将类型'bool(Class ::)(Class *)的'Class :: operator =='转换为类型'bool

我正在做一种方法来查看两张卡号是否相同

这是我的重载运算符和我称之为重载运算符的方法

bool Carta::operator==(Carta *c){
   if(getNumCarta()==c->getNumCarta() || getNumPal()==c->getNumPal()){
        return true;
    }
    else{
        return false;
    }
    //return(numCarta==c->numCarta)||(numPal!==c->numPal);
}

bool Carta::operator!=(Carta *c){
    if(getNumCarta()!=c->getNumCarta() && getNumPal()!=c->getNumPal()){
        return true;
    }
    else{
        return false;
    }
}

bool Table::pairs(){

    for(int i=1;i<posFull;i++){

        if(t[i-1]->operator == t[i+1]){
            return true;
        }
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

我期望返回一个真实的但永远不会发生

c++ class operator-overloading equality-operator

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

Powershell If then 语句不起作用。如何解决逻辑问题?

我似乎无法让这个“if then”语句起作用。这个简单的陈述我做错了什么。

$checkUS = "false"

$usRegions =(Get-EC2Region) |Where -Property RegionName -Like -Value "us-*" |select RegionName | foreach {$_.RegionName}
$allregions=(Get-EC2Region).RegionName

If($checkUS = "true") {$Regions=$usRegions} Else {$Regions=$allregions}
Run Code Online (Sandbox Code Playgroud)

谢谢!

powershell operators equality-operator

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

你如何评估 z = x- - == y + 1;

鉴于

int w = 1;
int x = 6;
int y = 5;
int z = 0;
 
z = !z || !x && !y;
printf("%d\n", z);

z = x-- == y + 1;
printf("%d\n", z);
Run Code Online (Sandbox Code Playgroud)

如果 x-- 是 5 并且 y+1 是 6,有人可以解释下面的行将如何评估为 1 吗?

z = x-- == y + 1;
Run Code Online (Sandbox Code Playgroud)

c decrement postfix-operator equality-operator

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

在if语句中使用==运算符两次

在java中这样做是否可以,是否有效?

if (turtles.get(h).getX() == turtles.get(g).getX() == 450) { 
    //stuff here
}
Run Code Online (Sandbox Code Playgroud)

基本上,我想检查X是否与Y相同,该值应为450.

java if-statement equality-operator

-1
推荐指数
2
解决办法
1259
查看次数

在没有语法错误的情况下,此代码中的if情况不起作用

if(s.name=="kolkata")      
{
    printf("the details");
}
if(strcmp((s.name,"kolkata")==0) 
{
    printf("the details");
}
Run Code Online (Sandbox Code Playgroud)

第一个"if"情况没有语法错误仍然无法正常工作,而第二个"if"情况确实非常有效,为什么?

c string strcmp equality-operator

-1
推荐指数
1
解决办法
71
查看次数

If 条件中的变量值 C++

我正在学习 C++ 编程语言,在某些时候陷入困境有人可以帮助我吗?问题是我在谷歌上搜索了一些东西,并了解到如果条件可以临时改变变量值。我的代码如下。

#include <iostream>

using namespace std;

int main()
{
    int a = 2;
    int b = a + 1;

    if ((a = 3) == b)
    {
        cout << a;
    }
    else
    {
        cout << a + 1;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,它打印 else 块,为什么不打印 if 块,条件必须为真?

c++ if-statement variable-assignment equality-operator

-1
推荐指数
1
解决办法
62
查看次数