簡體   English   中英

在集合中使用結構

[英]using struct in sets

我正在嘗試制作一組,在 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

執行此操作或修復錯誤的正確方法是什么?

inline bool operator<(const Config& lhs, const Config& rhs) { return lhs < rhs; }

這個operator<調用operator<調用operator< operator< operator<調用...你能發現問題嗎? 遞歸是無限的,最終會溢出堆棧。

您可能打算做的是將一方的成員與另一方的成員進行比較。 也就是說,讓編譯器完成工作會更容易(需要 C++20 或更高版本):

struct Config
{
    ... members ...
    friend auto operator<=>(const Config&, const Config&) = default;
};

比較字符串指針的另一個問題是不比較字符串的內容。 在這個簡單的例子中它可能不會引起問題,但是這個翻譯單元中的“Numbers”不一定與另一個翻譯單元中的“Numbers”具有相同的地址。 還有一個自動數組,例如char str[] = "Numbers"; 絕對不會有相同的地址。

要按內容比較字符串,您可以改用 std::string_view :

struct Config
{
    std::string_view lbl;
    std::string_view desc;

您的代碼的問題是您的 function 無限遞歸調用operator< function 。

要解決您的問題,您必須將 operator< function 替換為以下代碼:(如@Aconcagua 所說)

inline bool operator<(const Config& lhs, const Config& rhs)
{
    return lhs.lbl < rhs.lbl && 
        lhs.desc < rhs.desc && 
        lhs.se_2A < rhs.se_2A &&
        lhs.se_2B < rhs.se_2B &&
        lhs.se_2C < rhs.se_2C &&
        lhs.se_2D < rhs.se_2D &&
        lhs.se_2E < rhs.se_2E &&
        lhs.su_2A < rhs.su_2A &&
        lhs.su_2B < rhs.su_2B &&
        lhs.su_2C < rhs.su_2C &&
        lhs.su_2D < rhs.su_2D &&
        lhs.su_2E < rhs.su_2E &&
        lhs.total_size < rhs.total_size;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM