Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7702443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:13:42+00:00 2026-05-31T23:13:42+00:00

I want to use a C++ map structure, such as map<vector<DFSCode>, vector<PDB>> candidate ,

  • 0

I want to use a C++ map structure, such as map<vector<DFSCode>, vector<PDB>> candidate, DFSCode and PDB are two structures I define.

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

I will generate a lot of data which contain vector<DFSCode> and PDB, since one vector<DFSCode> may have many PDB, I want to use vector<PDB> to store them.
What I want to do is:

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

The first question is: Does above code satisfied my expectation that “one vector<DFSCode> contain many PDB“?

The second question is: I know I have to implement a comparable method of map, since I use vector<DFSCode> as my key, but I don’t know how to implement. I try to write one. But it seems not satisfied my expectation that “one vector<DFSCode> contain many PDB“, can anyone help me? 🙂

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
    }
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:13:43+00:00Added an answer on May 31, 2026 at 11:13 pm

    A vector of DFSCode can contain many DFSCode. Since a DFSCode can
    contain many DFS, a vector of DFSCode can contain many, many DFS.

    With regards to your code: some suggestions:

    • Use `push_back` and `pop_back`, rather than `resize`. It’s much more
      idiomatic. Your function `push` should start:

          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. This
    can 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 );
        }
    };
    

    EDIT:

    One important point I forgot to mention. With the above comparison
    operator, the order of items in the vectors is significant. If this is
    not acceptable, then you’ll probably have to end up sorting the elements
    first (in a temporary).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to the structure map but i want to use it in
I want to use a dictionary/map data structure in R, similar to Python's dict
I want to place markers on the map. I want to use the standard
For instance, if I want to map property Title I use: > Map(x =>
I want to use a pair from STL as a key of a map.
I want to have a map of vectors, (but I don't want to use
I've got a JSON string that I want to convert to a Map structure
I want to create a Hash Map (or another structure, if you have any
I want use groovy findAll with my param to filtering closure filterClosure = {
i want use some data from a website with web service. i have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.