自定义结构集声明中出错

Error in custom struct set declaration

本文关键字:出错 声明 结构 自定义      更新时间:2023-10-16

我遇到了一个奇怪的编译器错误,因为我刚开始将集合与自定义结构一起使用,所以我不确定到底是什么问题。

我正在尝试创建一组"配对",并使用自定义比较函数插入所述配对。

struct pairT {
    std::string first, second;
};
int PairCmp(pairT &one, pairT &two) {
    if (one.first < two.first && one.second == two.second) return 0;
    else if (one.first < two.first) return -1;
    else if (one.first == two.first && one.second < two.second) return -1;
    return 1;
}
std::set<pairT> CartesianProduct (std::set<std::string> &one, std::set<std::string> &two) {
    std::set<pairT> returnSet(PairCmp);
    /.../

我从最后一行代码中得到错误:C2664"无法将参数1从int转换为const std::less…blah,blah,blah。

关于我为什么被踢屁股,有什么建议吗?

使用对象(而不是指针)需要为std::set命名第二个模板参数,用于比较pairT的两个对象。有关示例,请参见std::less<>

此外,你在这里尝试的似乎是错误的。您试图在CartesianProduct()中返回一个std::set,但返回的PairCmp()返回一个整数。