无法插入集合
Trouble inserting to a set
我正在尝试将继承自 vector<string>
的 Tuple
添加到集合中。 (我读过这是不好的做法,但我的教授告诉我们这一点,并说对于这项作业,他希望我们无论如何都继承自 vector<string>
)我有一个地图,其中包含一个字符串作为键和一个 Relation
作为价值。 Relation
是我们为此作业构建的 class。在 Relation
中有一个 set<Tuple>
,其中 Tuple
本质上是一个 vector<string>
。我的问题是,当我尝试将 Tuple
添加到 Relation
的实例时,我无法这样做。
我第一次尝试添加到集合中时,集合的大小从 0 变为 1(插入成功),但在第一次之后的每次尝试中,大小都保持为 1(尝试失败)。
我已经包括了我也缩小了问题范围的代码区域,并包括了我认为需要的所有内容,但这是漫长的一天,所以如果我遗漏了什么,请原谅。
//Interpreter file
Database dataBase;
void Interpreter::createDatabase(){
for(unsigned int z = 0; z < dp.getFacts().size(); z++){
string relName = dp.getFacts()[z].getName();
vector<string> tup;
for(unsigned int i = 0; i < dp.getFacts()[z].getParams().size(); i++){
tup.push_back(dp.getFacts()[z].getParams()[i].getName());
}
dataBase.addTuple(relName,tup);
}
}
//in the Database.cpp file
map<string,Relation> db;
void Database::addTuple(string name, vector<string> tuple){
Relation temp = db.at(name);
temp.addTuple(tuple);
db.at(name) = temp;
}
// in the Relation.cpp File
set<Tuple> tuples;
void Relation::addTuple(vector<string> tuple){
Tuple t = Tuple(tuple);
tuples.insert(t);
}
// Tuple Class
vector<string> values;
Tuple::Tuple(){}
Tuple::Tuple(vector<string> val){
values = val;
}
Tuple::~Tuple(){}
string Tuple::toString(){
string str = "";
for(unsigned int i = 0; i < values.size(); i++){
str = str + values[i] + ".";
}
return str;
}
您可以使用调试器并观察
addTuple(...)
方法。
A set<...>
通常只接受独特的项目。可能您的 vector<string>
元组不是唯一的。
您也可以通过重载提供适当的比较运算符方法。
我正在尝试将继承自 vector<string>
的 Tuple
添加到集合中。 (我读过这是不好的做法,但我的教授告诉我们这一点,并说对于这项作业,他希望我们无论如何都继承自 vector<string>
)我有一个地图,其中包含一个字符串作为键和一个 Relation
作为价值。 Relation
是我们为此作业构建的 class。在 Relation
中有一个 set<Tuple>
,其中 Tuple
本质上是一个 vector<string>
。我的问题是,当我尝试将 Tuple
添加到 Relation
的实例时,我无法这样做。
我第一次尝试添加到集合中时,集合的大小从 0 变为 1(插入成功),但在第一次之后的每次尝试中,大小都保持为 1(尝试失败)。
我已经包括了我也缩小了问题范围的代码区域,并包括了我认为需要的所有内容,但这是漫长的一天,所以如果我遗漏了什么,请原谅。
//Interpreter file
Database dataBase;
void Interpreter::createDatabase(){
for(unsigned int z = 0; z < dp.getFacts().size(); z++){
string relName = dp.getFacts()[z].getName();
vector<string> tup;
for(unsigned int i = 0; i < dp.getFacts()[z].getParams().size(); i++){
tup.push_back(dp.getFacts()[z].getParams()[i].getName());
}
dataBase.addTuple(relName,tup);
}
}
//in the Database.cpp file
map<string,Relation> db;
void Database::addTuple(string name, vector<string> tuple){
Relation temp = db.at(name);
temp.addTuple(tuple);
db.at(name) = temp;
}
// in the Relation.cpp File
set<Tuple> tuples;
void Relation::addTuple(vector<string> tuple){
Tuple t = Tuple(tuple);
tuples.insert(t);
}
// Tuple Class
vector<string> values;
Tuple::Tuple(){}
Tuple::Tuple(vector<string> val){
values = val;
}
Tuple::~Tuple(){}
string Tuple::toString(){
string str = "";
for(unsigned int i = 0; i < values.size(); i++){
str = str + values[i] + ".";
}
return str;
}
您可以使用调试器并观察
addTuple(...)
方法。
A set<...>
通常只接受独特的项目。可能您的 vector<string>
元组不是唯一的。
您也可以通过重载提供适当的比较运算符方法。