2012-10-10 58 views
2

嘿傢伙我有一個問題與我的程序,我想閱讀一個語法,把多個右手規則放在使用多映射的左手規則中。問題是,讓我們說規則是: a - > al be ze 它映射[a,al]並忽略其餘部分。 只留下密鑰,我想把不同的屬性,以該鍵。 想知道你們是否可以發現我似乎找不到的錯誤。 我是否正確使用多重映射? 謝謝。使用多映射

map<string, string> rule; // global var 
void righthandside(){  // get rhs of grammar rule 

     char c; 
    skipSpace(); 
    c = getchar(); 
    if(isalpha(c)){ 
     checkforE = false; // rule not epsilon 
     while(isalnum(c)){ 
      righths += c; 
      c = getchar(); 
     } 
     righths += '\0'; 
     rule.insert(pair<string, string>(LHS[lhs], righths)); 
     righths.clear(); 
     righthandside(); 
    } 
    else if(c == '#'){ 
     if(checkforE == true) 
      rule.insert(pair<string, string>(LHS[lhs], epsilon)); // rule states NT goes to epsilon 
     skipSpace(); 
     c = getchar(); 

     if(c == '#'){   //end of file 
       cout << "end of file \n"; 
     } 

     else{     // end of rule 
      ungetc(c, stdin); 
      lhs++; 
      readGR(); 
     } 
    } 
    else{ 
     errorcode(0); 
    } 
} 

回答

0

如果你想與多個值的唯一的鍵,你可以使用std::map與容器的值,如:

std::map<std::string, std::vector<std::string> > rule; 

如果你想用一個數值重複鍵每次你也可以使用std::multimap這樣的:

std::multimap<std::string, std::string> rule; 

這將使rule遏制對[a,al][a,be][a,ze]