Skip to content

Instantly share code, notes, and snippets.

@SansPapyrus683
Last active March 17, 2024 02:21
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save SansPapyrus683/61b65d4d7ec223b48ebf5c3bb382ba8d to your computer and use it in GitHub Desktop.
Save SansPapyrus683/61b65d4d7ec223b48ebf5c3bb382ba8d to your computer and use it in GitHub Desktop.
A useful debugging template for C++.
#include <iostream>
#include <vector>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::vector<T>& vec) {
if (vec.empty()) {
out << "[]";
return out;
}
out << '[';
for (int i = 0; i < vec.size() - 1; i++) {
out << vec[i] << ", ";
}
return out << vec.back() << ']';
}
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& out, const std::pair<T1, T2>& pair) {
return out << '(' << pair.first << ", " << pair.second << ')';
}
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::deque<T>& deq) {
if (deq.empty()) {
out << "[]";
return out;
}
out << '[';
for (int i = 0; i < deq.size() - 1; i++) {
out << deq[i] << ", ";
}
return out << deq.back() << ']';
}
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& out, const std::unordered_map<T1, T2>& map) {
out << '{';
for (auto it = map.begin(); it != map.end(); it++) {
std::pair<T1, T2> element = *it;
out << element.first << ": " << element.second;
if (std::next(it) != map.end()) {
out << ", ";
}
}
return out << '}';
}
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& out, const std::map<T1, T2>& map) {
out << '{';
for (auto it = map.begin(); it != map.end(); it++) {
std::pair<T1, T2> element = *it;
out << element.first << ": " << element.second;
if (std::next(it) != map.end()) {
out << ", ";
}
}
return out << '}';
}
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::unordered_set<T>& set) {
out << '{';
for (auto it = set.begin(); it != set.end(); it++) {
T element = *it;
out << element;
if (std::next(it) != set.end()) {
out << ", ";
}
}
return out << '}';
}
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::multiset<T>& set) {
out << '{';
for (auto it = set.begin(); it != set.end(); it++) {
T element = *it;
out << element;
if (std::next(it) != set.end()) {
out << ", ";
}
}
return out << '}';
}
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::unordered_multiset<T>& set) {
out << '{';
for (auto it = set.begin(); it != set.end(); it++) {
T element = *it;
out << element;
if (std::next(it) != set.end()) {
out << ", ";
}
}
return out << '}';
}
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::set<T>& set) {
out << '{';
for (auto it = set.begin(); it != set.end(); it++) {
T element = *it;
out << element;
if (std::next(it) != set.end()) {
out << ", ";
}
}
return out << '}';
}
// Source: https://stackoverflow.com/a/31116392/12128483
template<typename Type, unsigned N, unsigned Last>
struct TuplePrinter {
static void print(std::ostream& out, const Type& value) {
out << std::get<N>(value) << ", ";
TuplePrinter<Type, N + 1, Last>::print(out, value);
}
};
template<typename Type, unsigned N>
struct TuplePrinter<Type, N, N> {
static void print(std::ostream& out, const Type& value) {
out << std::get<N>(value);
}
};
template<typename... Types>
std::ostream& operator<<(std::ostream& out, const std::tuple<Types...>& value) {
out << '(';
TuplePrinter<std::tuple<Types...>, 0, sizeof...(Types) - 1>::print(out, value);
return out << ')';
}
@Dipankar-Kumar-Singh
Copy link

vector<pair<int,int>> v ; don't work with gcc-12 or g++-12 , may you modify it so that vector of pairs also works .

@Dipankar-Kumar-Singh
Copy link

This is the best debugger header file which I found on internet till now . It is extremally powerful , thank you @SansPapyrus683 for this amazing file .

@SansPapyrus683
Copy link
Author

vector<pair<int,int>> v ; don't work with gcc-12 or g++-12 , may you modify it so that vector of pairs also works .

What's the error you get when you try to print this?

@Dipankar-Kumar-Singh
Copy link

vector<pair<int,int>> v ; don't work with gcc-12 or g++-12 , may you modify it so that vector of pairs also works .

What's the error you get when you try to print this?

Update : I think It it problem with new GCC-12.2 Compiler ON WINDOWS Platform Only [ from -> https://winlibs.com/ [ CodeForces uses this ] ] , But same thing on Linux Runs without error on same complier version [ Ubuntu gcc-12.2 ] .

Here is the error I am getting when using in Windows Environment [gcc - mingw64]
https://pastebin.com/nFkbAfJD

I think it is a bug with new compiler [ gcc-12 ] gcc on windows is not that as stable as Linux counterpart ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment