如何在C++映射中使用自定义结构
How to use self-defined structure in C++ Map
我想使用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
而不是map
。map
对于一个密钥只能有一个值。它类似于一对一关系。CCD_ 23可以对单个密钥具有多个值。
你不需要像安东尼奥建议的那样继承DFScode。
相关文章:
- C++ 通过函数传递自定义结构
- 在C++中循环访问自定义结构列表的小问题
- 如何在Qt中使用QDataStream将自定义结构保存/加载到二进制文件中?
- 结构向量中自定义结构函数的内存使用
- 类型转换自定义结构
- 使用结构绑定更改自定义结构的值
- 信号槽自定义结构问题
- 如何将unordered_set与自定义结构一起使用?
- 为什么C++在自定义结构向量时会出现 ostream 重载问题?
- 通过自定义结构和链接列表类中的C 中的哈希表
- 从自定义结构数组中提取数据
- 无法使用自定义结构的属性
- 通过引用将自定义结构的向量传递给 boost::compute 闭包或函数
- MPI_Op_create:候选功能不可行.自定义结构指针不能解释为空指针
- 使用 pybind11 将自定义结构(指针向量)从 C++ 传递到 python 的方法是什么?
- 在类构造函数中初始化自定义结构对象的正确方法
- STD ::原子和自定义结构的问题
- 正在取消引用自定义结构字段的迭代器
- 如何使用QSignalMapper发送自定义结构的实例
- 使用类定义的自定义结构