I am attempting to write a general utility code file that I can include whenever I want to for debugging purposes, instead of having to write the same code over and over again. Among those utility functions is an overloaded operator to output maps in C++.
template<typename T, typename U>
ostream &operator << (ostream &o, map<T, U> m) {
for (typename map<T, U>::iterator it = m.begin(); it != m.end(); it++) {
o << "{" << it->first << "} = [" << it->second << "]" << endl;
}
return o;
}
This function works, but for some reason the first element in the map always gets displayed last. For example, if I initialize a map with the following code:
map<string, int> test;
test["one"] = 1;
test["two"] = 2;
test["three"] = 3;
and then try to output it using the above function, it gets printed as:
{two} = [2]
{three} = [3]
{one} = [1]
It's not a huge deal since it's just output, but I'm wondering why this happens. Does this affect operations that are actually dependent on order, or is it just funky output?