stl_iterator.h

来自「symbian上STL模板库的实现」· C头文件 代码 · 共 773 行 · 第 1/2 页

H
773
字号
// Iterators -*- C++ -*-// Copyright (C) 2001, 2002, 2004 Free Software Foundation, Inc.//// This file is part of the GNU ISO C++ Library.  This library is free// software; you can redistribute it and/or modify it under the// terms of the GNU General Public License as published by the// Free Software Foundation; either version 2, or (at your option)// any later version.// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the// GNU General Public License for more details.// You should have received a copy of the GNU General Public License along// with this library; see the file COPYING.  If not, write to the Free// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,// USA.// As a special exception, you may use this file as part of a free software// library without restriction.  Specifically, if other files instantiate// templates or use macros or inline functions from this file, or you compile// this file and link it with other files to produce an executable, this// file does not by itself cause the resulting executable to be covered by// the GNU General Public License.  This exception does not however// invalidate any other reasons why the executable file might be covered by// the GNU General Public License./* * * Copyright (c) 1994 * Hewlett-Packard Company * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation.  Hewlett-Packard Company makes no * representations about the suitability of this software for any * purpose.  It is provided "as is" without express or implied warranty. * * * Copyright (c) 1996-1998 * Silicon Graphics Computer Systems, Inc. * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation.  Silicon Graphics makes no * representations about the suitability of this software for any * purpose.  It is provided "as is" without express or implied warranty. *//** @file stl_iterator.h *  This is an internal header file, included by other library headers. *  You should not attempt to use it directly. * *  This file implements reverse_iterator, back_insert_iterator, *  front_insert_iterator, insert_iterator, __normal_iterator, and their *  supporting functions and overloaded operators. */#ifndef _ITERATOR_H#define _ITERATOR_H 1namespace std{    // 24.4.1 Reverse iterators    /**     *  "Bidirectional and random access iterators have corresponding reverse     *  %iterator adaptors that iterate through the data structure in the     *  opposite direction.  They have the same signatures as the corresponding     *  iterators.  The fundamental relation between a reverse %iterator and its     *  corresponding %iterator @c i is established by the identity:     *  @code     *      &*(reverse_iterator(i)) == &*(i - 1)     *  @endcode     *     *  This mapping is dictated by the fact that while there is always a     *  pointer past the end of an array, there might not be a valid pointer     *  before the beginning of an array." [24.4.1]/1,2     *     *  Reverse iterators can be tricky and surprising at first.  Their     *  semantics make sense, however, and the trickiness is a side effect of     *  the requirement that the iterators must be safe.     */    template<typename _Iterator>        class reverse_iterator        : public iterator<typename iterator_traits<_Iterator>::iterator_category,        typename iterator_traits<_Iterator>::value_type,        typename iterator_traits<_Iterator>::difference_type,        typename iterator_traits<_Iterator>::pointer,        typename iterator_traits<_Iterator>::reference>    {        protected:            _Iterator current;        public:            typedef _Iterator					       iterator_type;            typedef typename iterator_traits<_Iterator>::difference_type                difference_type;            typedef typename iterator_traits<_Iterator>::reference   reference;            typedef typename iterator_traits<_Iterator>::pointer     pointer;        public:            /**             *  The default constructor default-initializes member @p current.             *  If it is a pointer, that means it is zero-initialized.             */            // _GLIBCXX_RESOLVE_LIB_DEFECTS            // 235 No specification of default ctor for reverse_iterator            reverse_iterator() : current() { }            /**             *  This %iterator will move in the opposite direction that @p x does.             */            explicit                reverse_iterator(iterator_type __x) : current(__x) { }            /**             *  The copy constructor is normal.             */            reverse_iterator(const reverse_iterator& __x)                : current(__x.current) { }            /**             *  A reverse_iterator across other types can be copied in the normal             *  fashion.             */            template<typename _Iter>                reverse_iterator(const reverse_iterator<_Iter>& __x)                : current(__x.base()) { }            /**             *  @return  @c current, the %iterator used for underlying work.             */            iterator_type                base() const                { return current; }            /**             *  @return  TODO             *             *  @doctodo             */            reference                operator*() const                {                    _Iterator __tmp = current;                    return *--__tmp;                }            /**             *  @return  TODO             *             *  @doctodo             */            pointer                operator->() const                { return &(operator*()); }            /**             *  @return  TODO             *             *  @doctodo             */            reverse_iterator&                operator++()                {                    --current;                    return *this;                }            /**             *  @return  TODO             *             *  @doctodo             */            reverse_iterator                operator++(int)                {                    reverse_iterator __tmp = *this;                    --current;                    return __tmp;                }            /**             *  @return  TODO             *             *  @doctodo             */            reverse_iterator&                operator--()                {                    ++current;                    return *this;                }            /**             *  @return  TODO             *             *  @doctodo             */            reverse_iterator operator--(int)            {                reverse_iterator __tmp = *this;                ++current;                return __tmp;            }            /**             *  @return  TODO             *             *  @doctodo             */            reverse_iterator                operator+(difference_type __n) const                { return reverse_iterator(current - __n); }            /**             *  @return  TODO             *             *  @doctodo             */            reverse_iterator&                operator+=(difference_type __n)                {                    current -= __n;                    return *this;                }            /**             *  @return  TODO             *             *  @doctodo             */            reverse_iterator                operator-(difference_type __n) const                { return reverse_iterator(current + __n); }            /**             *  @return  TODO             *             *  @doctodo             */            reverse_iterator&                operator-=(difference_type __n)                {                    current += __n;                    return *this;                }            /**             *  @return  TODO             *             *  @doctodo             */            reference                operator[](difference_type __n) const                { return *(*this + __n); }    };    //@{    /**     *  @param  x  A %reverse_iterator.     *  @param  y  A %reverse_iterator.     *  @return  A simple bool.     *     *  Reverse iterators forward many operations to their underlying base()     *  iterators.  Others are implemented in terms of one another.     *     */    template<typename _Iterator>        inline bool        operator==(const reverse_iterator<_Iterator>& __x,                const reverse_iterator<_Iterator>& __y)        { return __x.base() == __y.base(); }    template<typename _Iterator>        inline bool        operator<(const reverse_iterator<_Iterator>& __x,                const reverse_iterator<_Iterator>& __y)        { return __y.base() < __x.base(); }    template<typename _Iterator>        inline bool        operator!=(const reverse_iterator<_Iterator>& __x,                const reverse_iterator<_Iterator>& __y)        { return !(__x == __y); }    template<typename _Iterator>        inline bool        operator>(const reverse_iterator<_Iterator>& __x,                const reverse_iterator<_Iterator>& __y)        { return __y < __x; }    template<typename _Iterator>        inline bool        operator<=(const reverse_iterator<_Iterator>& __x,                const reverse_iterator<_Iterator>& __y)        { return !(__y < __x); }    template<typename _Iterator>        inline bool        operator>=(const reverse_iterator<_Iterator>& __x,                const reverse_iterator<_Iterator>& __y)        { return !(__x < __y); }    template<typename _Iterator>        inline typename reverse_iterator<_Iterator>::difference_type        operator-(const reverse_iterator<_Iterator>& __x,                const reverse_iterator<_Iterator>& __y)        { return __y.base() - __x.base(); }    template<typename _Iterator>        inline reverse_iterator<_Iterator>        operator+(typename reverse_iterator<_Iterator>::difference_type __n,                const reverse_iterator<_Iterator>& __x)        { return reverse_iterator<_Iterator>(__x.base() - __n); }    //@}    // 24.4.2.2.1 back_insert_iterator    /**     *  @brief  Turns assignment into insertion.     *     *  These are output iterators, constructed from a container-of-T.     *  Assigning a T to the iterator appends it to the container using     *  push_back.     *     *  Tip:  Using the back_inserter function to create these iterators can     *  save typing.     */    template<typename _Container>        class back_insert_iterator        : public iterator<output_iterator_tag, void, void, void, void>        {            protected:                _Container* container;            public:                /// A nested typedef for the type of whatever container you used.                typedef _Container          container_type;                /// The only way to create this %iterator is with a container.                explicit                    back_insert_iterator(_Container& __x) : container(&__x) { }                /**                 *  @param  value  An instance of whatever type                 *                 container_type::const_reference is; presumably a                 *                 reference-to-const T for container<T>.                 *  @return  This %iterator, for chained operations.                 *                 *  This kind of %iterator doesn't really have a "position" in the                 *  container (you can think of the position as being permanently at                 *  the end, if you like).  Assigning a value to the %iterator will                 *  always append the value to the end of the container.                 */                back_insert_iterator&                    operator=(typename _Container::const_reference __value)                    {                        container->push_back(__value);                        return *this;                    }                /// Simply returns *this.                back_insert_iterator&                    operator*()                    { return *this; }                /// Simply returns *this.  (This %iterator does not "move".)                back_insert_iterator&                    operator++()                    { return *this; }                /// Simply returns *this.  (This %iterator does not "move".)                back_insert_iterator                    operator++(int)                    { return *this; }        };    /**     *  @param  x  A container of arbitrary type.     *  @return  An instance of back_insert_iterator working on @p x.     *

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?