如何在C++映射中使用自定义结构

How to use self-defined structure in C++ Map

本文关键字:自定义 结构 映射 C++      更新时间:2023-10-16

我想使用C++映射结构,例如map<vector<DFSCode>, vector<PDB>> candidate、DFSCode和PDB是我定义的两个结构。

class DFS {
public:
    int from;
    int to;
    int fromlabel;
    int elabel;
    int tolabel;
    DFS(): from(0), to(0), fromlabel(0), elabel(0), tolabel(0) {};
};
struct DFSCode: public vector <DFS> {
public:
    void push (int from, int to, int fromlabel, int elabel, int tolabel)
    {
        resize (size() + 1);
        DFS &d = (*this)[size()-1];
        d.from = from;
        d.to = to;
        d.fromlabel = fromlabel;
        d.elabel = elabel;
        d.tolabel = tolabel;
    }
    void pop () { resize (size()-1); }
};
class PDB {
public:
    unsigned int tid;
    unsigned int gid;
    void push(int did, int vid, int vlabel)
    {
        tuple[did].vid = vid;
        tuple[did].vlabel = vlabel;
    }
    PDB(): tid(0), gid(0), tuple(0) {};
};

我会生成很多包含vector<DFSCode>PDB的数据,因为一个vector<DFSCode>可能有很多PDB,所以我想用vector<PDB>来存储它们。我想做的是:

vector<DFSCode> tempdfscodeList;
PDB             temppdb;
map<vector<DFSCode>, vector<PDB>> candidate;
for each `vector<DFSCode>` and `PDB` pair I generate
    candidate[tempdfscodeList].push_back(temppdb);

第一个问题是:上面的代码是否满足了我的期望,即"一个vector<DFSCode>包含多个PDB"?

第二个问题是:我知道我必须实现一个类似的map方法,因为我使用vector<DFSCode>作为密钥,但我不知道如何实现。我试着写一个。但"一个vector<DFSCode>包含多个PDB"似乎并不能满足我的期望,有人能帮我吗?:)

class dfscodeListCompare {  // compare vector<DFSCode>
public:
    bool operator() (const vector<DFSCode> &c1, const vector<DFSCode> &c2) const
    {
        for(int I = 0; I < c1.size(); I++) {
            if(c1[I].size() == c2[I].size()) {  // the size must be the same
                for(int j = 0; j < c1[I].size(); j++) {
                    if((c1[I][j].from != c2[I][j].from) || (c1[I][j].to != c2[I][j].to) || (c1[I][j].fromlabel != c2[I][j].fromlabel) || (c1[I][j].elabel != c2[I][j].elabel) || (c1[I][j].tolabel != c2[I][j].tolabel))
                        return false;   // if there exist one different
                }
            }
            else
                return false;
        }
        return true;    // pass all condition
    }
};

一个DFSCode的向量可以包含许多DFSCode。由于DFSCode可以包含许多DFS,一个DFSCode的向量可以包含许多、许多DFS

关于您的代码:一些建议:

  • 使用"push_back"answers"pop_back",而不是"resize"。更多惯用的您的功能"push"应该启动:
    push_back(DFS());back().from()=from;。。。
  • 为"DFS"提供一个构造函数,该构造函数接受它所需的参数:
    DFS::DFS(int from,int to,int fromLabel,int eLabel,int toLabel):来自(来自),到(到),fromLabel(fromLabel),电子标签(电子标签),toLabel(toLabel){}
    然后"push"变为:push_back(DFS(from、to、fromLabel、eLabel、toLabel));
  • 不要从"std::vector"继承。使其成为数据成员。

关于您关于排序函数的问题,CCD_ 18基本上是一个二维结构。这可以通过lexicographical_compare:优雅地处理

struct CompareDFSCode
{
    bool operator()( DFS const& lhs, DFS const& rhs ) const
    {
        if ( lhs.from != rhs.from )
            return lhs.from < rhs.from;
        else if ( lhs.to != rhs.to )
            return lhs.to < rhs.to;
        else if ( lhs.fromLabel != rhs.fromLabel )
            return lhs.fromLabel < rhs.fromLabel;
        else if ( lhs.eLabel != rhs.eLabel )
            return lhs.eLabel < rhs.eLabel;
        else
            return lhs.toLabel < rhs.toLabel;
    }
    bool operator()( DFSCode const& lhs, DFSCode const& rhs ) const
    {
        return std::lexicographical_compare(
            lhs,begin(), lhs.end(),
            rhs.begin(), rhs.end(),
            *this );
    }
    bool operator()(
            std::vector<DFSCode> const& lhs,
            std::vector<DFSCode> const& rhs ) const
    {
        return std::lexicographical_compare(
            lhs.begin(), lhs.end(),
            rhs.begin(), rhs.end(),
            *this );
    }
};

编辑:

有一点我忘了提。通过以上比较运算符,向量中项目的顺序是重要的。如果是不可接受,那么您可能最终不得不对元素进行排序首先(暂时)。

第一个问题是:上面的代码是否满足了我的期望,即"一个向量包含多个PDB"?

选择multimap而不是mapmap对于一个密钥只能有一个值。它类似于一对一关系。CCD_ 23可以对单个密钥具有多个值。

你不需要像安东尼奥建议的那样继承DFScode。