#include <iostream>
#include <map>
#include <vector>
#include <sstream>
using namespace std;
template<typename _Key, typename _Value>
std::ostream &operator<<(std::ostream &stream, const std::map<_Key, _Value> &mpObj)
{
size_t i = 0;
stream << "----> map.size=" << mpObj.size() << endl;
stream << "{" << endl;
for (const auto &item : mpObj) {
stream << "[" << i++ << "] key=" << item.first << ", value=" << item.second << endl;
}
stream << "}" << endl;
return stream;
}
template<typename _Value>
std::ostream &operator<<(std::ostream &stream, const std::vector<_Value> &vecObj)
{
size_t i = 0;
stream << "----> vector.size=" << vecObj.size() << std::endl;
stream << "{" << std::endl;
for (const auto &item : vecObj) {
stream << "[" << i++ << "] value=" << item << std::endl;
}
stream << "}" << std::endl;
return stream;
}
enum Color
{
color_red,
color_blue,
color_green
};
#ifndef PRINT_ENUM
#define PRINT_ENUM(enumKey) (int)enumKey<<"("<<#enumKey<<")"
#endif
std::ostream &operator<<(std::ostream &stream, const Color &obj)
{
switch (obj) {
case color_red:
stream << PRINT_ENUM(color_red);
break;
case color_blue:
stream << PRINT_ENUM(color_blue);
break;
case color_green:
stream << PRINT_ENUM(color_green);
break;
default:
stream << "error!!!(not enum Color)";
break;
}
return stream;
}
int main()
{
map<int, string> mp;
mp[1] = "a";
mp[2] = "b";
mp[3] = "c";
cout << "mp = " << mp << endl;
Color color = color_green;
cout << "color " << color << endl;
return 0;
}
mp = ----> map.size=3
{
[0] key=1, value=a
[1] key=2, value=b
[2] key=3, value=c
}
color 2(color_green)