1

これについてもっと簡単な方法はありますか、それとも私がひどく間違っていることがありますか? 私の問題の核心はコードにあると思いますが、

vector<int> &v = miss_words[*i];

しかし、多分私は全体の概念を間違っているだけです. 助言がありますか?

コード:

void print_map(map<string, vector<int> > miss_words)    // Prints out dictionary set
{
    map<string, vector<int> >::iterator it = miss_words.begin();    // Creates an iterator
    while(it != miss_words.end())           // While hasn't reached the end
    {
        vector<int> &v = miss_words[*it];   // Accesses Vector in map
        for(unsigned int g = 0; g <= v.size(); g++)
        {
            cout<<v.at(g)<<": ";            
            cout<<v.at(g)<<" "<<endl;   // Print out data at i
        }
        it++;                   // Increment iterator
    }
}

コンパイラは、「miss_words に「operator[]」に一致するものはありません。

4

1 に答える 1

4

あなたが言いたいvector<int> &v = it->second;

イテレータの値の型は、マップの値の型、つまりpair<string, vector<int>>.


実際、最新の C++ では、次のように、これをより単純に記述して、エラーを起こしにくくすることができます。

for (auto const & p : miss_words)
{ 
    for (auto const & x : p.second)
    {
        cout << x << ": " << x << " \n";
    }
}

そうすれば、ベクトルのサイズを気にする必要さえありません。

于 2013-07-02T23:32:48.430 に答える