C++
To use struct as std::map keys in cpp, we should implement operator<:
struct Person {
std::string name;
int age;
double height;
bool operator<(const Person& other) const {
if (name != other.name) return name < other.name;
if (age != other.age) return age < other.age;
return height < other.height;
}
};
Go
To use struct as treemap keys in go, we should implement interface Ordered:
type Ordered[T any] interface {
Less(another *T) bool
}
var _ Ordered[Person] = (*Person)(nil)
type Person struct {
Name string
Age int
Height float64
}
func (p *Person) Less(another *Person) bool {
if p.Name != another.Name {
return p.Name < another.Name
}
if p.Age != another.Age {
return p.Age < another.Age
}
return p.Height < another.Height
}
We should also create treemap.CustomKeyTreemap struct which accepts treemap.Ordered as key:
type CustomKeyTreemap[K Ordered[K], V any] struct {
...
}
C++
To use struct as
std::mapkeys in cpp, we should implementoperator<:Go
To use struct as treemap keys in go, we should implement interface
Ordered:We should also create
treemap.CustomKeyTreemapstruct which acceptstreemap.Orderedas key: