gpt4 book ai didi

c++ - 为什么在尝试将重复对象添加到 std::set 时会出现这个奇怪的错误?

转载 作者:太空宇宙 更新时间:2023-11-04 16:21:51 24 4
gpt4 key购买 nike

这是给我带来麻烦的代码:

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;
}

我一直想不通为什么会发生这种情况。我用这段代码得到了一个核心转储。

下面的代码可以很好地按字母顺序排列我的集合中的元组(它们是字符串 vector ),但我认为这是我错误的根源,因为当每个元素都相同时它不知道该怎么做:

编辑对代码进行了更改。

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;
}

};

'元组'来自以下代码:

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
}

关于c++ - 为什么在尝试将重复对象添加到 std::set 时会出现这个奇怪的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15757776/

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