使用结构向量存储在相同的索引中

时间:2014-02-19 06:19:37

标签: c++ vector struct

我在c ++上仍然相当生气,而且我无法理解我的问题。我遇到的问题是我想将lhs和rhs都存储到一个索引中直到读取HASH(当前将lhs存储到索引0并将rhs存储到索引1中),但我似乎无法找到另一种方式将rhs添加到我的结构向量中。我的代码:

struct Rules
{
string lhs;
vector<string> rhs;
}rule;

vector<Rules> ruleList;

int i = 0;
nextRule:
ttype = getToken();
if(ttype == ID)
{
    ruleList.push_back(rule);
    ruleList[i].lhs = token;
    ttype = getToken();
    if(ttype == ARROW)
    {
        ttype = getToken();
        if(ttype == ID)
        {
            ungetToken();
            while(ttype == ID)
            {
                ttype = getToken();
                if(ttype == HASH)
                {
                    i++;
                    goto nextRule;
                }
                rule.rhs.push_back(token); // PROBLEM IS HERE. Not stored in same index as lhs
            }
            ruleList.push_back(rule); // PROBLEM IS HERE
        }
    }
}

是否有'push_back'的替代品?

0 个答案:

没有答案