Skip to content

Instantly share code, notes, and snippets.

@robobenklein
Created April 3, 2018 19:56
Show Gist options
  • Save robobenklein/db130c269afda7fcb15de6b55fd0e84c to your computer and use it in GitHub Desktop.
Save robobenklein/db130c269afda7fcb15de6b55fd0e84c to your computer and use it in GitHub Desktop.
C++ debugging ostreams
// for cerr debugging, overloads for vector, list, set
template <class T>
std::ostream& operator<< (std::ostream& out, const std::vector<T>& v) {
if ( !v.empty() ) {
out << "vector[";
for (T item : v) {
out << item;
out << ", ";
}
out << "\b\b]";
}
return out;
}
template <class T>
std::ostream& operator<< (std::ostream& out, const std::list<T>& v) {
if ( !v.empty() ) {
out << "list[";
for (T item : v) {
out << item;
out << ", ";
}
out << "\b\b]";
}
return out;
}
template <class T>
std::ostream& operator<< (std::ostream& out, const std::set<T>& v) {
if ( !v.empty() ) {
out << "set[";
for (T item : v) {
out << item;
out << ", ";
}
out << "\b\b]";
}
return out;
}
template <typename T1, typename T2>
std::ostream& operator<< (std::ostream& out, const std::pair<T1, T2>& v) {
out << "pair(";
out << v.first;
out << ", ";
out << v.second;
out << ")";
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment