Re: stl multimap, insert with duplicate keys, is ordering stable?
reppisch wrote:
Hi Ng,
I have a multiset for keeping elements sorted in a container but key
values may also be equal.
Is there any guaranteed traversal order within the duplicate keys of a
multimap?
When inserting duplicate keys to the multiset i need output order to
be in insert order.
That's NOT how the multiset is defined in the Standard, but I believe you
can infer from the fact that the keys are ordered in non-descending order,
that the order of insertion of objects with the same value is preserved.
Sample:
std::multimap<int,char,std::greater<int> > m;
m.insert(pair<int,char>(1,'a'));
m.insert(pair<int,char>(2,'B'));
m.insert(pair<int,char>(2,'b'));
m.insert(pair<int,char>(2,'p'));
m.insert(pair<int,char>(3,'c'));
std::multimap<int,char,std::greater<int> >::iterator i = m.begin();
while (i != m.end()) {
cout << i->first << " " << i->second << endl;
++i;
};
- Is it guaranteed that the iteraton will allways print
3 c
2 B
2 b
2 p
1 a
Not directly.
- Can it be made deterministic by providing an insert hint like
m.insert(m.end(),pair<int,char>(2,'b'));
I did'nt find any information about this reading the sgi-specs.
Neither did I, but I think it can be inferred from the requirements
for the comparison object.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask