map<int, set<int> > map_set;
srand(time(0));
for (int i = 0; i < rand()%50 + 20; ++i) {
int k = rand()%10, v = rand()%10;
pair<map<int, set<int> >::iterator, bool> ret = map_set[k].insert(v);
if (!ret.second)
cout << "Attempted to insert [" << k << ":" << v << "], but entry already existed [" << ret.first->first << ":" << ret.first->second << "]" << endl;
}
我收到以下錯誤在我定義ret
行:錯誤試圖存儲時地圖的返回值插入
[Error] conversion from 'std::pair<std::_Rb_tree_const_iterator<int>, bool>' to non-scalar type 'std::pair<std::_Rb_tree_iterator<std::pair<const int, std::set<int> > >, bool>' requested
它看起來像編譯器告訴我,我沒有宣佈ret
作爲pair<map<int, set<int> >::iterator, bool>
,但作爲一些其他的事情,我不明白。
我不知道什麼是錯的,因爲在這種不同的方式做同樣的事情的作品:
for (int i = 0; i < rand()%50 + 20; ++i)
if (!(map_set[rand()%10].insert(rand()%10)).second)
cout << "Failed attempt to insert value for a new key: it already existed" << endl;
'ret'的類型是插入到'地圖>'。但'map_set [k] .insert(v)'插入到'set '中。 –
tenfour
這個問題似乎是脫離主題,因爲這不是一個幫助臺。 –