C++11 打印STL结构

#include <iostream>
#include <iterator>
#include <type_traits>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <tuple>
#include <list>
#include <sstream>

// This works similar to ostream_iterator, but doesn't print a delimiter after the final item
template <typename T, typename TChar = char, typename TCharTraits = std::char_traits<TChar>>
class pretty_ostream_iterator : public std::iterator<std::output_iterator_tag, void, void, void, void>
{
   
public:
    typedef TChar char_type;
    typedef TCharTraits traits_type;
    typedef std::basic_ostream<TChar, TCharTraits> ostream_type;

    pretty_ostream_iterator(ostream_type &stream, const char_type *delim = NULL)
        : _stream(&stream), _delim(delim), _insertDelim(false)
    {
   
    }

    pretty_ostream_iterator<T, TChar, TCharTraits> &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<T, TChar, TCharTraits> &operator*()
    {
   
        return *this;
    }

    pretty_ostream_iterator<T, TChar, TCharTraits> &operator++()
    {
   
        return *this;
    }

    pretty_ostream_iterator<T, TChar, TCharTraits> &operator++(int)
    {
   
        return *this;
    }

private:
    ostream_type *_stream;
    const char_type *_delim;
    bool _insertDelim;
};

// Basic is_container template; specialize to derive from std::true_type for all desired container types
template <typename T>
struct is_container : public std::false_type
{
   
};

// Mark vector as a container
template <typename T, typename TAllocator>
struct is_container<std::vector<T, TAllocator>> : public std::true_type
{
   
};

// Mark list as a container
template <typename T, typename 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值