Skip to content

Instantly share code, notes, and snippets.

@zorchp
Created December 23, 2025 05:40
Show Gist options
  • Select an option

  • Save zorchp/e0034536682bd6c4de8e558d78f15fd3 to your computer and use it in GitHub Desktop.

Select an option

Save zorchp/e0034536682bd6c4de8e558d78f15fd3 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <type_traits>
// Helper to check if a type is a container (excluding string)
template <typename T, typename = void>
struct is_container : std::false_type {};
template <typename T>
struct is_container<T, std::void_t<decltype(std::begin(std::declval<T&>())),
decltype(std::end(std::declval<T&>()))>>
: std::integral_constant<bool, !std::is_same_v<T, std::string>> {};
// Generic operator<<
template <typename T>
std::enable_if_t<is_container<T>::value, std::ostream&> operator<<(
std::ostream& os, const T& container) {
os << "["s;
auto it = std::begin(container);
auto end = std::end(container);
while (it != end) {
os << *it; // Recursive call
auto next = std::next(it);
if (next != end) {
if constexpr (is_container<typename T::value_type>::value) {
os << "\n"s;
} else {
os << " "s;
}
}
it = next;
}
os << "]"s;
return os;
}
void t1() {
//
vector<vector<int>> vv{
{1, 2},
{2, 3},
{2, 3},
{2, 3},
};
cout << vv;
}
int main() {
t1();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment