C++打印图结构、结构、构造函数>类型
我目前拥有以下代码:C++打印图结构、结构、构造函数>类型,c++,loops,map,struct,constructor,C++,Loops,Map,Struct,Constructor,我目前拥有以下代码: struct LR0Item{ LR0Item(const string& lhs_p, vector<string> rhs_p, int dpos_p) : lhs(lhs_p), rhs(rhs_p), dpos(dpos_p) {} string lhs; vector<string> rhs; int dpos; }; struct Node{ Node( LR0Item* lr)
struct LR0Item{
LR0Item(const string& lhs_p, vector<string> rhs_p, int dpos_p)
: lhs(lhs_p), rhs(rhs_p), dpos(dpos_p) {}
string lhs;
vector<string> rhs;
int dpos;
};
struct Node{
Node( LR0Item* lr) : item(lr) {}
LR0Item* item;
map<string, Node*> tr;
};
struct fizz{
bool operator()(
const LR0Item &a,
const LR0Item &b) {
if(a.lhs != b.lhs)
return a.lhs<b.lhs;
if(a.dpos != b.dpos)
return a.dpos<b.dpos;
return a.rhs<b.rhs;
}
};
vector<Node*> N;
map<LR0Item,Node*,fizz> nmap;
我有一些杂项代码用数据填充nmap。我想知道我如何能打印出一个好格式的nmap数据。另外,我也不完全确定“fizz”在做什么。我将给您一个提示,而不是编写整个代码:
typedef map<LR0Item,Node*,fizz> Mymap;
Mymap::iterator it = nmap.begin();
for(;it != nmap.end() ;++it) {
//it->first is your key of type LR0Item
//it->second is your value of type Node*
LR0Item key = it->first ;
Node* val_ptr = it->second;
/*
Now use key.lhs, --> std::string
key.rhs, --> std::vector
key.dpos --> int
And
val_ptr->item, --> LR0Item*
val_ptr->tr --> map of std::string as key and Node* as its value
*/
}
同时,我也不能完全确定“fizz”在做什么
fizz是一个用作自定义比较器的函子,用于将元素插入地图nmap中
看
基本上它首先比较:
字符串lhs,如果相等,则
int dpo,如果相等,则
矢量rhs
通过以良好的格式打印,您的意思是如何横切nmap?您应该检查map的文档,看看fizz在做什么:@P0W是的,我的意思是横切nmap,为清晰起见进行了编辑。我确实有一个问题,当循环通过val_ptr->tr时,我的程序在大约1次迭代后意外崩溃。我该如何访问这个“tr”地图?每当我试图访问val_ptr->tr时,我的程序就会崩溃,有什么想法吗?@Dohrann你可能应该在一篇新帖子上问这个问题