在集合中使用结构体
我正在尝试制作一组集合,在 C++ 中执行此操作的正确方法是什么?我想要实现的目标是这样的
One = { {"DDD", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64},{"JJ", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64},
{"kk", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64}, {"LL", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64} };
我尝试过这样的
#include <set>
#include <iostream>
#include <algorithm>
#include <cstring>
struct Config
{
const char* lbl;
const char* desc;
std::uint8_t se_2A;
std::uint8_t se_2B;
std::uint8_t se_2C;
std::uint8_t se_2D;
std::uint8_t se_2E;
std::uint8_t su_2A;
std::uint8_t su_2B;
std::uint8_t su_2C;
std::uint8_t su_2D;
std::uint8_t su_2E;
std::size_t total_size;
};
inline bool operator<(const Config& lhs, const Config& rhs)
{
return lhs < rhs;
}
int main(){
Config b1 = {"DDD", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64};
Config b2 = {"JJ", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64};
Config b3 = {"kk", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64};
Config b4 = {"LL", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64};
std::set<Config> newConfig;
std::set<std::set<Config>> One;
newConfig.insert(b1);
newConfig.insert(b2);
}
但是给了我这个错误
Program received signal SIGSEGV, Segmentation fault.
0x00005555555555b9 in operator< (
lhs=<error reading variable: Cannot access memory at address 0x7fffff7feff8>, rhs=<error reading variable: Cannot access memory at address 0x7fffff7feff0>)
at main.cpp:32
执行此操作或修复错误的正确方法是什么?
I am trying to make a set of sets, what is the correct to method to do that in C++. What i am trying to achieve is something like this
One = { {"DDD", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64},{"JJ", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64},
{"kk", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64}, {"LL", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64} };
I tried like this
#include <set>
#include <iostream>
#include <algorithm>
#include <cstring>
struct Config
{
const char* lbl;
const char* desc;
std::uint8_t se_2A;
std::uint8_t se_2B;
std::uint8_t se_2C;
std::uint8_t se_2D;
std::uint8_t se_2E;
std::uint8_t su_2A;
std::uint8_t su_2B;
std::uint8_t su_2C;
std::uint8_t su_2D;
std::uint8_t su_2E;
std::size_t total_size;
};
inline bool operator<(const Config& lhs, const Config& rhs)
{
return lhs < rhs;
}
int main(){
Config b1 = {"DDD", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64};
Config b2 = {"JJ", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64};
Config b3 = {"kk", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64};
Config b4 = {"LL", "Numbers", 0xf, 0xf, 0xf, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 64};
std::set<Config> newConfig;
std::set<std::set<Config>> One;
newConfig.insert(b1);
newConfig.insert(b2);
}
But gives me this error
Program received signal SIGSEGV, Segmentation fault.
0x00005555555555b9 in operator< (
lhs=<error reading variable: Cannot access memory at address 0x7fffff7feff8>, rhs=<error reading variable: Cannot access memory at address 0x7fffff7feff0>)
at main.cpp:32
Whats the correct to way to do this or fix the error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此
operator<
调用operator<
,后者调用operator<
,而operator<
又调用operator
;
哪个调用...你能发现问题吗?递归是无限的,最终会溢出堆栈。您可能想要做的是将一侧的成员与另一侧的成员进行比较。也就是说,让编译器完成这项工作会更容易(需要 C++20 或更高版本):
比较指针与字符串的另一个问题不是比较字符串的内容。在这个简单的例子中,它可能不会引起问题,但是这个翻译单元中的“Numbers”不一定与另一个翻译单元中的“Numbers”具有相同的地址。而像
char str[] = "Numbers";
这样的自动数组肯定不会有相同的地址。要按内容比较字符串,您可以使用 std::string_view 代替:
This
operator<
calls theoperator<
which calls theoperator<
which theoperator<
which theoperator<
which calls... can you spot the problem? The recursion is infinite and will eventually overflow the stack.What you're probably intending to do is to compare the members of one side to the members of the other side. That said, it's easier to let the compiler do the work (C++20 or later required):
Another issue that comparing pointers to strings isn't comparing the contents of the string. It might not cause problems in this trivial example, but "Numbers" in this translation unit doesn't necessarily have the same address as "Numbers" in another translation unit. And an automatic array such as
char str[] = "Numbers";
definitely wouldn't have the same address.To compare the strings by their content, you could use std::string_view instead:
您的代码的问题是您的函数无限递归地调用 operator< 函数。
要解决您的问题,您必须更换您的运营商<具有以下代码的函数:(如@Aconcagua所说)
The problem with your code is that your function calls operator< function recursively infinitely.
To fix your issue, you'll have to replace your operator< function with the below code: (as said by @Aconcagua)