2013-12-08 75 views
0

我现在有以下代码:C++ - 印刷地图<结构,结构,构造>类型

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)。我也不完全确定'嘶嘶声'在做什么。

+0

通过在 “好” 的格式打印,你的意思是如何横穿'nmap'? – P0W

+0

你应该查看'map'的文档来看看“fizz”在做什么:http://www.cplusplus.com/reference/map/map/ – leewz

+0

@PW是的,我的意思是横切nmap,为了清晰起见编辑。 – Dohrann

回答

0

我会给你一个提示,而不是写整个代码:“还我不能完全肯定,什么‘嘶嘶’是做”

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是用作自定义比较器插入元件插入地图算符nmap

参见this

基本上它首先比较:

  • lhs,如果相等,则通过

  • int dpos,如果相等,则通过

  • 矢量rhslexicographically

+0

我有一个问题,当通过val_ptr-> tr这是一个地图<字符串,节点*>循环时,我得到我的程序意外崩溃1次左右迭代后。我如何访问这个'tr'地图? – Dohrann

+0

任何时候我尝试访问val_ptr-> tr我的程序崩溃了,有什么想法? – Dohrann

+0

@Dohrann你可能应该在新帖子上提问 – P0W