我正在解決加速C++中的問題 我們有一個程序,它將字符串存儲在向量中並根據一定的規則進行輸出。 原始代碼片段如下。如何在C++中訪問列表中的元素
typedef vector<string> Rule;
typedef vector<Rule> Rule_collection;
typedef map<string, Rule_collection> Grammar;
// read a grammar from a given input stream
Grammar read_grammar(istream& in)
{
Grammar ret;
string line;
// read the input
while (getline(in, line)) {
// `split' the input into words
vector<string> entry = split(line);
if (!entry.empty())
// use the category to store the associated rule
ret[entry[0]].push_back(
Rule(entry.begin() + 1, entry.end()));
}
return ret;
}
然後我們要求使用列表而不是向量。
因此,上述所有向量將成爲列表。
我堅持的是如何處理行開始ret [entry]] push_back等。還有什麼要在規則後... 我已經嘗試使用entry.begin()但那doesn沒有工作。
任何援助將不勝感激。
?他們全部? – 2011-02-01 15:41:29
`開始`實際工作:-) – 2011-02-01 15:41:35
是的,這是正確的larsmans。我認爲作者希望我們瞭解向量和列表的工作方式之間的區別。 – 2011-02-01 15:51:34