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
}
};
0
A
回答
1
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;
}
1
你的谓语必须提供运营商,如:
struct Compare
{
bool operator() (const T1& lhs, const T2& rhs)
{
// here's the comparison logic
return bool_value;
}
};
,并指定其为一组的比较:
std::set<Tuple, Compare> tupleSet;
1
使用
bool operator < (const Tuple &tup) const
{
/*if(values < tup.getStringVec())
return true;*/ //getStringVec undefined, so comment out temporarily
return false;
}
相关问题
- 1. std :: map不接受我的运营商<
- 2. 的Javascript运营商<< and >>
- 3. 运营商<< , >>重载失败特定类
- 4. 运营商<<在派生类C++
- 5. 运营商<<(ostream&os,...)模板类
- 6. 运营商<< and >>重载
- 7. 即使我超载`运营商<<`
- 8. 我想超载运营商<<
- 9. 转换F#管道运营商(<|, >>,<<)来OCaml的
- 10. 运营商'!'不能应用于类型'<null>
- 11. 运营商<<超载
- 12. 超载运营商<<
- 13. 运营商<<重载
- 14. 虚拟<<运营商
- 15. 运营商<<在C#
- 16. Ruby:<< - 运营商
- 17. ==运营商的成本vs < or >运营商
- 18. 方法类似<< and >>运营商没有松动位
- 19. 全球运营商<<和成员运营商之间的共存<<
- 20. Haskell's Parsec的问题<|>运营商
- 21. 错误不符合运营商<<
- 22. 的std ::流的读/写操作VS << /运营商>>
- 23. QVector <T>运营商=和运营商[]
- 24. stringstream的运营商<<一个模板类型
- 25. 私人朋友运营商的代理类<<
- 26. 超负荷运营商<<在C++类中的
- 27. Func <>运营商在纯英文
- 28. 什么运营商<>在VBA
- 29. MySQL <=>运营商做什么?
- 30. Sybase:BETWEEN与“<=" and "> =”运营商
并以 “不接受”,你是什么意思?你得到什么样的错误信息? –
您的操作符需要为'const',如在'bool operator <(const Tuple&)const'中,而fyi,您需要为'std :: set'实现的唯一一个操作符是'operator <',因为它使用严格的弱顺序,并且'!(a WhozCraig
但是,如果你要实现一个,你应该全部实现它们。如果'operator <'对您的课程有意义,那么所有其他比较运算符也是如此。如果'operator <'*对你的类没有意义,那么你不应该为了满足'std :: set'而实现它,你应该使用一个单独的命名比较器,名称描述它的功能。 –