Pretty-print C++ STL containers

前端 未结 10 1732
谎友^
谎友^ 2020-11-21 07:39

Please take note of the updates at the end of this post.

Update: I have created a public project on GitHub for this library!


10条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-21 08:07

    This solution was inspired by Marcelo's solution, with a few changes:

    #include 
    #include 
    #include 
    #include 
    #include 
    
    // This works similar to ostream_iterator, but doesn't print a delimiter after the final item
    template >
    class pretty_ostream_iterator : public std::iterator
    {
    public:
        typedef TChar char_type;
        typedef TCharTraits traits_type;
        typedef std::basic_ostream ostream_type;
    
        pretty_ostream_iterator(ostream_type &stream, const char_type *delim = NULL)
            : _stream(&stream), _delim(delim), _insertDelim(false)
        {
        }
    
        pretty_ostream_iterator& operator=(const T &value)
        {
            if( _delim != NULL )
            {
                // Don't insert a delimiter if this is the first time the function is called
                if( _insertDelim )
                    (*_stream) << _delim;
                else
                    _insertDelim = true;
            }
            (*_stream) << value;
            return *this;
        }
    
        pretty_ostream_iterator& operator*()
        {
            return *this;
        }
    
        pretty_ostream_iterator& operator++()
        {
            return *this;
        }
    
        pretty_ostream_iterator& operator++(int)
        {
            return *this;
        }
    private:
        ostream_type *_stream;
        const char_type *_delim;
        bool _insertDelim;
    };
    
    #if _MSC_VER >= 1400
    
    // Declare pretty_ostream_iterator as checked
    template
    struct std::_Is_checked_helper > : public std::tr1::true_type
    {
    };
    
    #endif // _MSC_VER >= 1400
    
    namespace std
    {
        // Pre-declarations of container types so we don't actually have to include the relevant headers if not needed, speeding up compilation time.
        // These aren't necessary if you do actually include the headers.
        template class vector;
        template class list;
        template class set;
        template class map;
    }
    
    // Basic is_container template; specialize to derive from std::true_type for all desired container types
    template struct is_container : public std::false_type { };
    
    // Mark vector as a container
    template struct is_container > : public std::true_type { };
    
    // Mark list as a container
    template struct is_container > : public std::true_type { };
    
    // Mark set as a container
    template struct is_container > : public std::true_type { };
    
    // Mark map as a container
    template struct is_container > : public std::true_type { };
    
    // Holds the delimiter values for a specific character type
    template
    struct delimiters_values
    {
        typedef TChar char_type;
        const TChar *prefix;
        const TChar *delimiter;
        const TChar *postfix;
    };
    
    // Defines the delimiter values for a specific container and character type
    template
    struct delimiters
    {
        static const delimiters_values values; 
    };
    
    // Default delimiters
    template struct delimiters { static const delimiters_values values; };
    template const delimiters_values delimiters::values = { "{ ", ", ", " }" };
    template struct delimiters { static const delimiters_values values; };
    template const delimiters_values delimiters::values = { L"{ ", L", ", L" }" };
    
    // Delimiters for set
    template struct delimiters, char> { static const delimiters_values values; };
    template const delimiters_values delimiters, char>::values = { "[ ", ", ", " ]" };
    template struct delimiters, wchar_t> { static const delimiters_values values; };
    template const delimiters_values delimiters, wchar_t>::values = { L"[ ", L", ", L" ]" };
    
    // Delimiters for pair
    template struct delimiters, char> { static const delimiters_values values; };
    template const delimiters_values delimiters, char>::values = { "(", ", ", ")" };
    template struct delimiters, wchar_t> { static const delimiters_values values; };
    template const delimiters_values delimiters, wchar_t>::values = { L"(", L", ", L")" };
    
    // Functor to print containers. You can use this directly if you want to specificy a non-default delimiters type.
    template, typename TDelimiters = delimiters >
    struct print_container_helper
    {
        typedef TChar char_type;
        typedef TDelimiters delimiters_type;
        typedef std::basic_ostream& ostream_type;
    
        print_container_helper(const T &container)
            : _container(&container)
        {
        }
    
        void operator()(ostream_type &stream) const
        {
            if( delimiters_type::values.prefix != NULL )
                stream << delimiters_type::values.prefix;
            std::copy(_container->begin(), _container->end(), pretty_ostream_iterator(stream, delimiters_type::values.delimiter));
            if( delimiters_type::values.postfix != NULL )
                stream << delimiters_type::values.postfix;
        }
    private:
        const T *_container;
    };
    
    // Prints a print_container_helper to the specified stream.
    template
    std::basic_ostream& operator<<(std::basic_ostream &stream, const print_container_helper &helper)
    {
        helper(stream);
        return stream;
    }
    
    // Prints a container to the stream using default delimiters
    template
    typename std::enable_if::value, std::basic_ostream&>::type
        operator<<(std::basic_ostream &stream, const T &container)
    {
        stream << print_container_helper(container);
        return stream;
    }
    
    // Prints a pair to the stream using delimiters from delimiters>.
    template
    std::basic_ostream& operator<<(std::basic_ostream &stream, const std::pair &value)
    {
        if( delimiters, TChar>::values.prefix != NULL )
            stream << delimiters, TChar>::values.prefix;
    
        stream << value.first;
    
        if( delimiters, TChar>::values.delimiter != NULL )
            stream << delimiters, TChar>::values.delimiter;
    
        stream << value.second;
    
        if( delimiters, TChar>::values.postfix != NULL )
            stream << delimiters, TChar>::values.postfix;
        return stream;    
    }
    
    // Used by the sample below to generate some values
    struct fibonacci
    {
        fibonacci() : f1(0), f2(1) { }
        int operator()()
        {
            int r = f1 + f2;
            f1 = f2;
            f2 = r;
            return f1;
        }
    private:
        int f1;
        int f2;
    };
    
    int main()
    {
        std::vector v;
        std::generate_n(std::back_inserter(v), 10, fibonacci());
    
        std::cout << v << std::endl;
    
        // Example of using pretty_ostream_iterator directly
        std::generate_n(pretty_ostream_iterator(std::cout, ";"), 20, fibonacci());
        std::cout << std::endl;
    }
    

    Like Marcelo's version, it uses an is_container type trait that must be specialized for all containers that are to be supported. It may be possible to use a trait to check for value_type, const_iterator, begin()/end(), but I'm not sure I'd recommend that since it might match things that match those criteria but aren't actually containers, like std::basic_string. Also like Marcelo's version, it uses templates that can be specialized to specify the delimiters to use.

    The major difference is that I've built my version around a pretty_ostream_iterator, which works similar to the std::ostream_iterator but doesn't print a delimiter after the last item. Formatting the containers is done by the print_container_helper, which can be used directly to print containers without an is_container trait, or to specify a different delimiters type.

    I've also defined is_container and delimiters so it will work for containers with non-standard predicates or allocators, and for both char and wchar_t. The operator<< function itself is also defined to work with both char and wchar_t streams.

    Finally, I've used std::enable_if, which is available as part of C++0x, and works in Visual C++ 2010 and g++ 4.3 (needs the -std=c++0x flag) and later. This way there is no dependency on Boost.

提交回复
热议问题