#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
C++11 打印STL结构
最新推荐文章于 2025-03-09 12:26:06 发布