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
}
};
A vector of
DFSCodecan contain manyDFSCode. Since aDFSCodecancontain many
DFS, a vector ofDFSCodecan contain many, manyDFS.With regards to your code: some suggestions:
idiomatic. Your function `push` should start:
push_back( DFS() ); back().from() = from; ...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 ) );
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: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).