使用STL模板必须重载的运算符

STL各种容器和算法的sort和find函数对重载运算符的调用情况:

1) 二叉树类型的容器的sort和find都会调用operator < 。

2)线性类型容器sort会调用operator <;线性容器使用std::find会调用operator ==。

需要非常注意重载<运算符,分类讨论要周全。不然重则会导致dump机问题,轻则会导致排序不对。

对于< 号的重载要满足严格弱序的要求。

严格弱序定义如下:

 x R x 为false

x R y => !(y R x)   另一种表示为    !(x R y) && !(y R x) 则 x=y

 x R y 且 y R z 则 x R z


下面调用find 函数测试在set中find调用了< 关系,(去掉注释,调用stl 标准的find函数会调用 == 关系)

#include <iostream>
#include <set>
using namespace std;
 struct s{
    int x,y;
    s(int xx,int yy):x(xx),y(yy){}
    bool operator <(const s& tmp) const {
        if(x < tmp.x){
            return true;
        }
        else if(y < tmp.y){
            return true;
        }
        return false;
    }
 };
set<s> ms;
int main(){
        ms.insert(s(31,41));
        s test(31,41);
        s test2(32,41);
        if(test < test2){
            cout<<" test<test2"<<endl;
        }
        //auto itt = find(ms.begin(),ms.end(),test);
        auto itt = ms.find(test);
        if(itt!= ms.end()){
            cout<< "find the value: "<<itt->x<<endl;
        }
        else{
             cout<<"not found"<<endl;
        }
}

Building wheel for quickjs (setup.py) ... error error: subprocess-exited-with-error × python setup.py bdist_wheel did not run successfully. │ exit code: 1 ╰─> [12 lines of output] running bdist_wheel running build running build_py creating build\lib.win-amd64-cpython-313\quickjs copying quickjs\__init__.py -> build\lib.win-amd64-cpython-313\quickjs running build_ext building '_quickjs' extension creating build\temp.win-amd64-cpython-313\Release creating build\temp.win-amd64-cpython-313\Release\upstream-quickjs "D:\Visual Studio\Visual Studio 2022\Community\VC\Tools\MSVC\14.43.34808\bin\HostX86\x64\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -DCONFIG_VERSION=\"2021-03-27\" -DCONFIG_BIGNUM -IE:\Python3\include -IE:\Python3\Include "-ID:\Visual Studio\Visual Studio 2022\Community\VC\Tools\MSVC\14.43.34808\include" "-ID:\Visual Studio\Visual Studio 2022\Community\VC\Tools\MSVC\14.43.34808\ATLMFC\include" "-ID:\Visual Studio\Visual Studio 2022\Community\VC\Auxiliary\VS\include" "-ID:\Windows Kits\10\include\10.0.22621.0\ucrt" "-ID:\Windows Kits\10\\include\10.0.22621.0\\um" "-ID:\Windows Kits\10\\include\10.0.22621.0\\shared" "-ID:\Windows Kits\10\\include\10.0.22621.0\\winrt" "-ID:\Windows Kits\10\\include\10.0.22621.0\\cppwinrt" /Tcmodule.c /Fobuild\temp.win-amd64-cpython-313\Release\module.obj -Werror=incompatible-pointer-types cl: 命令行 error D8021 :无效的数值参数“/Werror=incompatible-pointer-types” error: command 'D:\\Visual Studio\\Visual Studio 2022\\Community\\VC\\Tools\\MSVC\\14.43.34808\\bin\\HostX86\\x64\\cl.exe' failed with exit code 2 [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for quickjs Running setup.py clean for quickjs Failed to build quickjs ERROR: Failed to build installable wheels for some pyproject.toml based projects (quickjs)
03-23
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值