set<>类,当我插入类时,它不接受我的<运算符
set<> class, when I insert into the class, it doesn't accept my < operator
class Tuple
{
private:
vector<string> values;
public:
Tuple(vector<Parameter> newValues)
{
for(int i = 0; i < newValues.size(); i++)
{
string val = newValues[i].getValue();
values.push_back(val);
}
}
Tuple(vector<string> newAttributes)
{
values = newAttributes;
}
~Tuple()
{
}
bool operator < (Tuple &tup)
{
if(values < tup.getStringVec())
return true;
return false;
}
bool operator <= (Tuple &tup)
{
if(values <= tup.getStringVec())
return true;
return false;
}
bool operator > (Tuple &tup)
{
if(values > tup.getStringVec())
return true;
return false;
}
bool operator >= (Tuple &tup)
{
if(values >= tup.getStringVec())
return true;
return false;
}
};
class Relation
{
private:
set<Tuple> tupleSet;
public:
Relation():
{
}
~Relation()
{
}
void addToTupleSet(Tuple newTuple)
{
tupleSet.insert(newTuple); //<<this causes the problem
}
};
std::set
的默认比较器使用 std::less<T>
,这要求对象暴露在某种形式的operator <
中。这通常是以下两种形式之一:
一个自由函数,像这样:
bool operator <(const Tuple& arg1, const Tuple& arg2);
或成员函数,如下所示:
class Tuple
{
public:
bool operator <(const Tuple& arg) const
{
// comparison code goes here
}
};
如果您不想实现operator <
只是为了在std::set
中使用,您当然可以直接实现自己的二进制比较器类型,并将其用作std::less<T>
的比较器替代方案。你是否这样做是你的决定,也是对不同问题的不同解决方案(即如何做到这一点,Niyaz 在另一个答案中涵盖了这个问题)。
您的代码,稍微修改一下,以免吸入命名空间std
并在适当的地方使用引用(顺便说一句,您可能想看看这些代码,因为它们将显着减少您来回复制数据所花费的时间)。
#include <iostream>
#include <string>
#include <iterator>
#include <vector>
#include <set>
// I added this, as your source included no such definition
class Parameter
{
public:
Parameter(const std::string s) : s(s) {}
const std::string& getValue() const { return s; }
private:
std::string s;
};
class Tuple
{
private:
std::vector<std::string> values;
public:
Tuple(const std::vector<Parameter>& newValues)
{
for(auto val : newValues)
values.push_back(val.getValue());
}
Tuple(const std::vector<std::string>& newAttributes)
: values(newAttributes)
{
}
// note const member and parameter. neither the passed object nor
// this object should be modified during a comparison operation.
bool operator < (const Tuple &tup) const
{
return values < tup.values;
}
};
class Relation
{
private:
std::set<Tuple> tupleSet;
public:
void addToTupleSet(const Tuple& tup)
{
tupleSet.insert(tup);
}
};
int main(int argc, char *argv[])
{
Tuple tup({"a","b","c"});
Relation rel;
rel.addToTupleSet(tup);
return 0;
}
下面
用于运算符"<"
bool operator < (const Tuple &tup) const
{
/*if(values < tup.getStringVec())
return true;*/ //getStringVec undefined, so comment out temporarily
return false;
}
您的谓词必须提供如下运算符:
struct Compare
{
bool operator() ( const T1& lhs, const T2& rhs )
{
// here's the comparison logic
return bool_value;
}
};
并将其指定为集合的比较器:
std::set<Tuple, Compare> tupleSet;
相关文章:
- 为什么线程不接受此输入?
- 函数不接受 X 参数,函数使用默认参数
- C++ boost::asio::ip::tcp::acceptor 有时不接受连接器?
- B不接受8作为输入的是什么?C++
- C++数组输入不接受一定数量的整数
- C++概念assignable_from不接受 const&-return 运算符=
- 斯堪夫不接受输入
- C++字符数组不接受超过 4 个字符的输入
- CIN不接受C++中带有空格的输入?
- 为什么C++ STL 哈希表 (unordered_map) 不接受向量作为键
- 为什么不接受具有默认分配参数的函数作为 0-arg 生成器?
- 模板接受常量,但不接受文字
- 如何在构造函数中传递 const 引用时强制编译器不接受右值
- 为什么 strtok_r() 只接受字符数组而不接受字符指针
- C++ cout 不接受字符串或带 + 的字符串
- std::cin不接受输入
- 带有openSSL的libwebsocket服务器不接受连接
- 函数不接受 C++ 参数
- 将任意数据传递给不接受"void* userarg"的C++回调
- C++ 实例化新对象时不接受继承方法默认参数值