If I do this:
#include <map>
#include <iostream>
int main()
{
std::multimap<char, int> m;
m.emplace('a', 100);
m.emplace('b', 200);
m.emplace('b', 201);
m.emplace('c', 300);
for (const auto& p : m)
std::cout << p.first << '\t' << p.second << '\n';
}
… then, since C++11, I'm guaranteed that the element with value 200
will precede the element with value 201
.
But what if I do this?
#include <map>
#include <iostream>
int main()
{
std::multimap<char, int> m{
{'a', 100},
{'b', 200},
{'b', 201},
{'c', 300}
};
for (const auto& p : m)
std::cout << p.first << '\t' << p.second << '\n';
}
Are we guaranteed that "insertion order" matches the order of elements in the initialiser?
A quick test gives encouraging results, but doesn't really prove anything.
I'm writing C++17.
(I could switch to std::map
with a compound key, but I have thousands of these things managed by a common interface, and only a few contain duplicate keys so I'm hesitant to introduce that complexity overall.)