gpt4 book ai didi

C++映射指针变量排序

转载 作者:行者123 更新时间:2023-11-30 03:31:42 25 4
gpt4 key购买 nike

你好我想知道如何对Tkey变量为指针类型的 map 进行排序。有返回 char* 类型的 getName 函数。所以我试着与 strcmp 进行比较。但是返回部分有一些错误。

    struct Compare_P {
inline bool operator()(Person const& a, Person const& b) {
return (strcmp(a.getName(), b.getName())) < 0;
}
};
map<Person*, House*, Compare_P>A_List;

最佳答案

map 的键是 Person*,但是 Compare_P::operator() 采用 Person const&。您可以通过定义

map<Person, House, Compare_P> A_List;

或通过正确的Compare_P

struct Compare_P {
bool operator()(Person const* a, Person const* b) {
return (strcmp(a->getName(), b->getName())) < 0;
}

关于C++映射指针变量排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44003267/

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