我想使用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可以
包含许多DFSDFSCode的向量可以包含许多DFS

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


使用`push_back`和`pop_back`,而不是`resize`。还有更多
惯用的。您的函数“ push”应该开始:

    push_back( DFS() );
    back().from() = from;
    ...
  • Give `DFS` a constructor which takes the arguments it needs:
        DFS::DFS( int from, int to, int fromLabel, int eLabel, int toLabel )
            : from( from )
            , to( to )
            , fromLabel( fromLabel )
            , eLabel( eLabel)
            , toLabel( toLabel )
        {
        }
    
    Then `push` becomes simply: push_back( DFS( from, to, fromLabel, eLabel, toLabel ) );
  • Don't inherit from `std::vector`. Make it a data member.
  • With regards to your question about the ordering function,std::vector<DFSCode> is basically a two dimensional structure. Thiscan be handled elegantly by means of 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 );
        }
    };
    


    编辑:

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

    09-06 18:20