Skip to content

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:

map<int, int> mp{
            {1, 1},
            {2, 4},
            {3, 9}};
    cout << mp << endl;

The output shows:

{ 1 => 1, 2 => 4, 3 => 9 }

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~

Comments