📄 stl_iterator.h
字号:
/*
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Copyright (c) 1996-1998
* Silicon Graphics Computer Systems, Inc.
*
* Copyright (c) 1997
* Moscow Center for SPARC Technology
*
* Copyright (c) 1999
* Boris Fomitchev
*
* This material is provided "as is", with absolutely no warranty expressed
* or implied. Any use is at your own risk.
*
* Permission to use or copy this software for any purpose is hereby granted
* without fee, provided the above notices are retained on all copies.
* Permission to modify the code and to distribute modified code is granted,
* provided the above notices are retained, and a notice that the code was
* modified is included with the above copyright notice.
*
*/
/* NOTE: This is an internal header file, included by other STL headers.
* You should not attempt to use it directly.
*/
#ifndef __SGI_STL_INTERNAL_ITERATOR_H
#define __SGI_STL_INTERNAL_ITERATOR_H
#ifndef __SGI_STL_INTERNAL_ITERATOR_BASE_H
# include <stl_iterator_base.h>
#endif
// streambuf_iterators predeclarations must appear first
#ifndef __STLPORT_IOSFWD
# include <iosfwd>
#endif
__STL_BEGIN_NAMESPACE
template <class _Container>
class back_insert_iterator {
protected:
_Container* container;
public:
typedef _Container container_type;
typedef output_iterator_tag iterator_category;
#ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
#endif
explicit back_insert_iterator(_Container& __x) : container(&__x) {}
back_insert_iterator<_Container>&
operator=(const typename _Container::value_type& __value) {
container->push_back(__value);
return *this;
}
back_insert_iterator<_Container>& operator*() { return *this; }
back_insert_iterator<_Container>& operator++() { return *this; }
back_insert_iterator<_Container>& operator++(int) { return *this; }
};
#ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
template <class _Container>
inline output_iterator_tag
iterator_category(const back_insert_iterator<_Container>&)
{
return output_iterator_tag();
}
#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
template <class _Container>
inline back_insert_iterator<_Container> back_inserter(_Container& __x) {
return back_insert_iterator<_Container>(__x);
}
template <class _Container>
class front_insert_iterator {
protected:
_Container* container;
public:
typedef _Container container_type;
typedef output_iterator_tag iterator_category;
# ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
# endif
explicit front_insert_iterator(_Container& __x) : container(&__x) {}
front_insert_iterator<_Container>&
operator=(const typename _Container::value_type& __value) {
container->push_front(__value);
return *this;
}
front_insert_iterator<_Container>& operator*() { return *this; }
front_insert_iterator<_Container>& operator++() { return *this; }
front_insert_iterator<_Container>& operator++(int) { return *this; }
};
#ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
template <class _Container>
inline output_iterator_tag
iterator_category(const front_insert_iterator<_Container>&)
{
return output_iterator_tag();
}
#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
template <class _Container>
inline front_insert_iterator<_Container> front_inserter(_Container& __x) {
return front_insert_iterator<_Container>(__x);
}
template <class _Container>
class insert_iterator {
protected:
_Container* container;
typename _Container::iterator iter;
public:
typedef _Container container_type;
typedef output_iterator_tag iterator_category;
# ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
# endif
insert_iterator(_Container& __x, typename _Container::iterator __i)
: container(&__x), iter(__i) {}
insert_iterator<_Container>&
operator=(const typename _Container::value_type& __value) {
iter = container->insert(iter, __value);
++iter;
return *this;
}
insert_iterator<_Container>& operator*() { return *this; }
insert_iterator<_Container>& operator++() { return *this; }
insert_iterator<_Container>& operator++(int) { return *this; }
};
#ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
template <class _Container>
inline output_iterator_tag
iterator_category(const insert_iterator<_Container>&)
{
return output_iterator_tag();
}
#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
template <class _Container, class _Iterator>
inline
insert_iterator<_Container> inserter(_Container& __x, _Iterator __i)
{
typedef typename _Container::iterator __iter;
return insert_iterator<_Container>(__x, __iter(__i));
}
// istream_iterator and ostream_iterator look very different if we're
// using new, templatized iostreams than if we're using the old cfront
// version.
#if defined (__STL_USE_NEW_IOSTREAMS)
# if (defined (__STL_USE_OWN_NAMESPACE) || defined (__STLPORT_NEW_IOSTREAMS) || !defined(__STL_USE_NAMESPACES))
# ifndef __STL_LIMITED_DEFAULT_TEMPLATES
template <class _Tp,
class _CharT = __STL_DEFAULTCHAR, class _Traits = char_traits<_CharT>,
class _Dist = ptrdiff_t>
# define __ISI_TMPL_HEADER_ARGUMENTS class _Tp, class _CharT, class _Traits, class _Dist
# define __ISI_TMPL_ARGUMENTS _Tp, _CharT, _Traits, _Dist
# else
# if defined (__STL_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && ! defined (__STL_DEFAULT_TYPE_PARAM)
# define __ISI_TMPL_HEADER_ARGUMENTS class _Tp
# define __ISI_TMPL_ARGUMENTS _Tp
template <class _Tp>
# else
# define __ISI_TMPL_HEADER_ARGUMENTS class _Tp, class _Dist
# define __ISI_TMPL_ARGUMENTS _Tp, _Dist
template <class _Tp,__DFL_TYPE_PARAM(_Dist, ptrdiff_t)>
# endif /* __STL_MINIMUM_DEFAULT_TEMPLATE_PARAMS */
# endif /* __STL_LIMITED_DEFAULT_TEMPLATES */
class istream_iterator {
# ifdef __STL_LIMITED_DEFAULT_TEMPLATES
typedef char _CharT;
typedef char_traits<char> _Traits;
# if defined (__STL_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && ! defined (__STL_DEFAULT_TYPE_PARAM)
typedef ptrdiff_t _Dist;
# endif
# endif
typedef istream_iterator< __ISI_TMPL_ARGUMENTS > _Self;
public:
typedef _CharT char_type;
typedef _Traits traits_type;
typedef basic_istream<_CharT, _Traits> istream_type;
typedef input_iterator_tag iterator_category;
typedef _Tp value_type;
typedef _Dist difference_type;
typedef const _Tp* pointer;
typedef const _Tp& reference;
istream_iterator() : _M_stream(0), _M_ok(false) {}
istream_iterator(istream_type& __s) : _M_stream(&__s) { _M_read(); }
reference operator*() const { return _M_value; }
pointer operator->() const { return &(operator*()); }
_Self& operator++() {
_M_read();
return *this;
}
_Self operator++(int) {
_Self __tmp = *this;
_M_read();
return __tmp;
}
bool _M_equal(const _Self& __x) const
{ return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream); }
private:
istream_type* _M_stream;
_Tp _M_value;
bool _M_ok;
void _M_read() {
_M_ok = (_M_stream && *_M_stream) ? true : false;
if (_M_ok) {
*_M_stream >> _M_value;
_M_ok = *_M_stream ? true : false;
}
}
};
#ifndef __STL_LIMITED_DEFAULT_TEMPLATES
template <class _Tp,
class _CharT = __STL_DEFAULTCHAR, class _Traits = char_traits<_CharT> >
#else
template <class _Tp>
#endif
class ostream_iterator {
# ifdef __STL_LIMITED_DEFAULT_TEMPLATES
typedef char _CharT;
typedef char_traits<char> _Traits;
typedef ostream_iterator<_Tp> _Self;
# else
typedef ostream_iterator<_Tp, _CharT, _Traits> _Self;
# endif
public:
typedef _CharT char_type;
typedef _Traits traits_type;
typedef basic_ostream<_CharT, _Traits> ostream_type;
typedef output_iterator_tag iterator_category;
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
ostream_iterator(ostream_type& __s) : _M_stream(&__s), _M_string(0) {}
ostream_iterator(ostream_type& __s, const _CharT* __c)
: _M_stream(&__s), _M_string(__c) {}
_Self& operator=(const _Tp& __value) {
*_M_stream << __value;
if (_M_string) *_M_stream << _M_string;
return *this;
}
_Self& operator*() { return *this; }
_Self& operator++() { return *this; }
_Self& operator++(int) { return *this; }
private:
ostream_type* _M_stream;
const _CharT* _M_string;
};
// We do not read any characters until operator* is called. The first
// time operator* is called, it calls getc. Subsequent calls to getc
// return a cached character, and calls to operator++ use snextc. Before
// operator* or operator++ has been called, _M_is_initialized is false.
template<class _CharT, class _Traits>
class istreambuf_iterator
{
public:
typedef _CharT char_type;
typedef _Traits traits_type;
typedef typename _Traits::int_type int_type;
typedef basic_streambuf<_CharT, _Traits> streambuf_type;
typedef basic_istream<_CharT, _Traits> istream_type;
typedef input_iterator_tag iterator_category;
typedef _CharT value_type;
typedef typename _Traits::off_type difference_type;
typedef const _CharT* pointer;
typedef const _CharT& reference;
public:
istreambuf_iterator(streambuf_type* __p = 0) { this->_M_init(__p); }
istreambuf_iterator(istream_type& __is) { this->_M_init(__is.rdbuf()); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -