gpt4 book ai didi

c++ - 我是否需要对 K 类进行完整排序才能使用 std::map

转载 作者:搜寻专家 更新时间:2023-10-31 00:00:34 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
What requirements must std::map key classes meet to be valid keys?

我想使用 std::map作为从我的类(class)到另一个类(class)的 map 。如果我尝试以下代码,我会收到错误“undefined operator <”。这是否意味着我需要在类里面订购 K使用 map ?它必须是完整的订单吗?我需要所有四个排序运算符还是 >够了吗?

#include <iostream>
#include <map>
#include <stdio.h>
using namespace std;

struct K {
int i, j;

K(int i, int j) : i(i), j(j){}

friend bool operator==(const K& x, const K& y){ return (x.i==y.i)&&(x.j==y.j); }
friend bool operator!=(const K& x, const K& y){ return !(x==y); }

/* friend bool operator<(const K&x, const K&y){
if(x.i<y.i) return true;
if(x.i>y.i) return false;
return x.j<y.j;
}
friend bool operator>(const K&x, const K&y){ return y<x; }
friend bool operator<=(const K&x, const K&y){ return !(y<x); }
friend bool operator>=(const K&x, const K&y){ return !(x<y); }
*/
};


int main(){
map<K, float> m;
m[K(1,2)]=5.4;
if(m.find(K(1,2))!=m.end())
cout << "Found: " << m[K(1,2)] << endl;
else
cout << "Not found" << endl;
return 0;
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com