template <typename T>
std::ostream& operator << (std::ostream& out, std::vector<T> vec) {
out << "[";
for (typename std::vector<T>::iterator ite(vec.begin()); ite != vec.end(); ite++) {
out << *ite;
if (vec.end() != ite + 1)
out << ", ";
}
out << "]";
return out;
}
用法
std::vector<int> v = {1,2,3};
std::cout << v << std::endl;
std::vector<std::vector<int>> vec = {{1,2,3},{4,5,6}};
std::cout << vec << std::endl;
打印map
#include <iostream>
#include <map>
#include <utility>
#include <algorithm>
using namespace std;
template<typename T, typename U>
ostream& operator <<(ostream& os, const map<T, U> &m) {
for_each(m.begin(), m.end(), [&os](pair<T, U> p) {os << "map[" << p.first << "]=" << p.second << endl; });
return os;
}
int main() {
map<int, int> m;
m[0] = 1;
m[1] = 2;
cout << m << endl;
}
网友评论