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;