我目前有以下代码:
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‘在做什么。
发布于 2013-12-08 11:23:10
我将给您一个提示,而不是编写整个代码:
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,如果等于,则按dpos,如果等于,则由rhs 词典学https://stackoverflow.com/questions/20452401
复制相似问题