gpt4 book ai didi

c++ - 自定义结构集声明错误

转载 作者:行者123 更新时间:2023-11-28 08:10:46 24 4
gpt4 key购买 nike

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

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

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...等等,等等,等等。

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

最佳答案

使用对象(而不是指针)要求您为 std::set 命名第二个模板参数用于比较 pairT 的两个对象.参见 std::less<> 举个例子。

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

关于c++ - 自定义结构集声明错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9148461/

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