为什么我得到这个奇怪的错误时,试图添加一个重复的对象std::set
Why am I getting this strange error when trying to add a duplicate object to a std::set?
这是给我麻烦的代码:
Relation* Relation::relation_union(Relation* r) {
std::set<Tuple*>::iterator it1;
for (it1 = tuples.begin(); it1 != tuples.end(); it1++) {
r->tuples.insert(*it1); //this is where i insert into the set
}
return r;
}
我不明白为什么这种事会发生在我的生活中。我用这段代码得到一个核心转储。
下面的代码可以很好地按字母顺序排列我的集合中的元组(这是字符串的向量),但我认为这是我的错误的来源,因为它不知道当每个元素都是相同的时候该怎么做:
EDIT对代码进行修改。
struct comp {
bool operator ()(const Tuple * lt, const Tuple * rt) {
for (unsigned i = 0; i < lt->values.size(); i++) {
std::string strl = lt->values[i];
std::string strr = rt->values[i];
if (strl != strr) {
return (strl < strr); // compares with the length
}
}
return false;
}
};
'tuples'来自以下代码:
Relation(){
name = "";
schema = new Schema();
tuples = std::set<Tuple*, comp>();
domain = std::set<std::string>();
}
std::string name;
Schema* schema;
std::set<Tuple*, comp> tuples;
std::set<std::string> domain;
}
这是我的堆栈反向跟踪-它对我不是很有帮助:
Program received signal SIGABRT, Aborted.
0x00007ffff753b425 in raise () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) bt
#0 0x00007ffff753b425 in raise () from /lib/x86_64-linux-gnu/libc.so.6
#1 0x00007ffff753eb8b in abort () from /lib/x86_64-linux-gnu/libc.so.6
#2 0x00007ffff7b9169d in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff7b8f846 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff7b8f873 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff7b8f96e in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x00007ffff7b3c987 in std::__throw_out_of_range(char const*) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7 0x00007ffff7b7a453 in std::string::substr(unsigned long, unsigned long) const () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#8 0x000000000040d889 in Input::getTokensValue (this=0x630460) at Input.cpp:91
#9 0x000000000040e812 in Lex::emit (this=0x7fffffffe130, tokenType=UNDEFINED) at Lex.cpp:268
#10 0x000000000040e12d in Lex::nextState (this=0x7fffffffe130) at Lex.cpp:106
#11 0x000000000040e026 in Lex::generateTokens (this=0x7fffffffe130, input=0x630460) at Lex.cpp:85
#12 0x000000000040da20 in Lex::Lex (this=0x7fffffffe130, filename=0x0) at Lex.cpp:17
#13 0x000000000040ea3e in main (argc=1, argv=0x7fffffffe248) at main.cpp:7
任何帮助都将非常感激。谢谢。
bool operator ()(const Tuple * lt, const Tuple * rt) {
for (unsigned i = 0; i < lt->values.size(); i++) {
std::string strl = lt->values[i];
std::string strr = rt->values[i];
if (strl != strr) {
return (strl < strr); // compares with the length
}
}
return false;//EDIT
}
如果所有值相等,则comp::operator()
应返回false
。
struct comp {
bool operator ()(const Tuple * lt, const Tuple * rt) {
for (unsigned i = 0; i < lt->values.size(); i++) {
std::string strl = lt->values[i];
std::string strr = rt->values[i];
if (strl != strr) {
return (strl < strr); // compares with the length
}
}
return false;
}
};
您的比较运算符期望两个操作数的'values'成员具有相同的大小。如果rt->values比lt->values短,您将得到另一个越界错误(但问题不是来自这里)。
相关文章:
- 是否有一个 std::set 函数来确定不超过数字 x 的最大元素?
- 我可以制作一个std::set的constexpr对象吗
- 创建 std::set 只复制一个元素,如何解决这个问题?
- 如何键入定义一个专门的 std::set 模板,使用特定的比较函数实例化
- 为什么传递给函数 set::itrator 而不是 const_iterator 违反了一个定义规则?
- 给定一个 std::set::iterator,获取一个指向下一个元素的迭代器
- STD :: SET(或STD :: MAP)是否有一个提供基于值的查找的STL比较器?STD :: lands_les
- std::set的替代品(可以将元素从一个集合移动到另一个集合)
- 获取第 n 项与 std::set 中第一个/最后一个的偏移量
- 模板化的使用是否为具有自定义键的unordered_map/set创建了一个良好的模式
- Cmake错误:set命令不喜欢上一个set命令中的相同.h文件
- 如何将 std::set 的元素添加到另一个非空集
- 如何在另一个类数据成员上定义“std::set”排序
- 为什么 std::set<>::find 返回一个 const?
- 返回一个带有 "return std::set<int>()" 的空集 - 为什么它会运行?
- 在C#中创建一个STL std::set并封送到C++
- 我应该做一个类,而不是重复的get/set语句
- 从std::vector存储到std::set,其中vector包含一个结构体,而std::set只包含该结构体中的一个
- 擦除 std::set 的最后一个成员
- 在c++中实现一个set模板的添加和删除