Last updated:2026-10-12
Quick STL Container Output in C++¶
I made some modifications to Mr. Wu's code — click here to go straight to the code
Input:
The output shows:
¶
How to Integrate¶
Download output_container.h into your project and include it:
#include "output_container.h"
int main() {
std::vector<int> vec{1, 2, 3, 4, 5};
std::set<std::string> names{"light", "city"};
std::cout << vec << std::endl; // { 1, 2, 3, 4, 5 }
std::cout << names << std::endl; // { city, light }
return 0;
}
Which Containers Are Supported¶
vector, list, set, map, multiset, multimap, the unordered family, pair — plus any custom type that is iterable (anything implementing begin/end); nested containers print directly too.
The Principle in One Sentence¶
The core is specializing operator<< for containers: use SFINAE (void_t detecting begin()/end()) to decide "is it iterable" — if yes, do element-wise output; if not, fall back to the original output stream. If you are interested, read the source directly: under two hundred lines, great material for learning the expression-detection idiom.
Printing containers while debugging is a real necessity; this little tool saves you countless hand-written for loops. Questions are welcome in the comments~
Related Topics¶
- Building C++ Projects with Bazel | C++那些事
- Debugging Bazel Builds with LLDB | C++那些事
- CMake Essentials in Practice | C++那些事
- Getting Started with the Conan C++ Package Manager | C++那些事
- C++ Insights Compile-Time Observation | C++那些事
- Interactive C++ in Jupyter Notebook | C++那些事
- C++ string to int | C++那些事
- The C++ dbg-macro Debugging Macro | C++那些事
- GCC Compilation Flags Explained | C++那些事
- Debugging C++ Programs with GDB | C++那些事