结构体的比较

1. 重写 == 操作符
  
#include<iostream>
using namespace std;

struct A{
    char ch;
    int val;
    friend bool operator==(const A &ob1, const A &ob2);
    bool operator==(const A &rhs);
};

bool operator==(const A &ob1, const A &ob2){
    return (ob1.ch == ob2.ch && ob1.val == ob2.val);
}
bool A::operator==(const A &rhs){
    return (ch == rhs.ch && val == rhs.val);
}

int main(){
    struct A s3,s4;
    A  s, s1;
    A s2;
    s1.ch = 1;
    s1.val = 2;
    s2.ch = 1;
    s2.val = 2;
    if (s1 == s2){
        cout << "s1 is equal with s2" << endl;
    }
    else{
        cout << "s1 is not equal with s2" << endl;
    }
    return 0;
}
2. 对结构体的变量进行一一比较
3. 对结构体指针进行比较,如果保存的是同一实例,两个结构体的指针保存的地址是一样的
 
原文地址:https://www.cnblogs.com/wanghao-boke/p/14641453.html