开发者

Why set<string>::iterator cannot use as key of map?

I have a piece of code like this:

set<string>::iterator it1;
set<string>::iterator it2;
pair<set<string>::iterator,bool> ret;

set<string> s;
ret = s.insert("bbbb1");
it1 = ret.first;

ret = s.insert("bbbb2");
it2 = ret.first;

map<set<string>::iterator, set<string>::iterator> m;

m.insert(make_pair(it1,it2));

but the last line "m.ins开发者_Go百科ert(make_pair(it1,it2));" failed..


std::set iterators are not random access iterators so they are not less-than comparable.

The type that you use as a key in a std::map must be able to be sorted using a strict weak ordering. By default, std::map uses < to order keys. You can change this behavior by providing a comparator when you define the type of the std::map. You'll probably want to perform some relational comparison using the object pointed to by the iterator.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜