stl_vector.h

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

H
933
字号
         *         *  Note that this kind of operation could be expensive for a         *  %vector and if it is frequently used the user should         *  consider using std::list.         */        template<typename _InputIterator>            void            insert(iterator __position, _InputIterator __first,                    _InputIterator __last)            {                // Check whether it's an integral type.  If so, it's not an iterator.                typedef typename _Is_integer<_InputIterator>::_Integral _Integral;                _M_insert_dispatch(__position, __first, __last, _Integral());            }        /**         *  @brief  Remove element at given position.         *  @param  position  Iterator pointing to element to be erased.         *  @return  An iterator pointing to the next element (or end()).         *         *  This function will erase the element at the given position and thus         *  shorten the %vector by one.         *         *  Note This operation could be expensive and if it is         *  frequently used the user should consider using std::list.         *  The user is also cautioned that this function only erases         *  the element, and that if the element is itself a pointer,         *  the pointed-to memory is not touched in any way.  Managing         *  the pointer is the user's responsibilty.         */        iterator            erase(iterator __position);        /**         *  @brief  Remove a range of elements.         *  @param  first  Iterator pointing to the first element to be erased.         *  @param  last  Iterator pointing to one past the last element to be         *                erased.         *  @return  An iterator pointing to the element pointed to by @a last         *           prior to erasing (or end()).         *         *  This function will erase the elements in the range [first,last) and         *  shorten the %vector accordingly.         *         *  Note This operation could be expensive and if it is         *  frequently used the user should consider using std::list.         *  The user is also cautioned that this function only erases         *  the elements, and that if the elements themselves are         *  pointers, the pointed-to memory is not touched in any way.         *  Managing the pointer is the user's responsibilty.         */        iterator            erase(iterator __first, iterator __last);        /**         *  @brief  Swaps data with another %vector.         *  @param  x  A %vector of the same element and allocator types.         *         *  This exchanges the elements between two vectors in constant time.         *  (Three pointers, so it should be quite fast.)         *  Note that the global std::swap() function is specialized such that         *  std::swap(v1,v2) will feed to this function.         */        void            swap(vector& __x)            {                std::swap(this->_M_impl._M_start, __x._M_impl._M_start);                std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);                std::swap(this->_M_impl._M_end_of_storage, __x._M_impl._M_end_of_storage);            }        /**         *  Erases all the elements.  Note that this function only erases the         *  elements, and that if the elements themselves are pointers, the         *  pointed-to memory is not touched in any way.  Managing the pointer is         *  the user's responsibilty.         */        void            clear() { erase(begin(), end()); }        protected:        /**         *  @if maint         *  Memory expansion handler.  Uses the member allocation function to         *  obtain @a n bytes of memory, and then copies [first,last) into it.         *  @endif         */        template<typename _ForwardIterator>            pointer            _M_allocate_and_copy(size_type __n,                    _ForwardIterator __first, _ForwardIterator __last)            {                pointer __result = this->_M_allocate(__n);                try                {                    std::uninitialized_copy(__first, __last, __result);                    return __result;                }                catch(...)                {                    _M_deallocate(__result, __n);                    __throw_exception_again;                }            }        // Internal constructor functions follow.        // Called by the range constructor to implement [23.1.1]/9        template<typename _Integer>            void            _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type)            {                this->_M_impl._M_start = _M_allocate(__n);                this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;                this->_M_impl._M_finish = std::uninitialized_fill_n(this->_M_impl._M_start,                        __n, __value);            }        // Called by the range constructor to implement [23.1.1]/9        template<typename _InputIterator>            void            _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,                    __false_type)            {                typedef typename iterator_traits<_InputIterator>::iterator_category                    _IterCategory;                _M_range_initialize(__first, __last, _IterCategory());            }        // Called by the second initialize_dispatch above        template<typename _InputIterator>            void            _M_range_initialize(_InputIterator __first,                    _InputIterator __last, input_iterator_tag)            {                for ( ; __first != __last; ++__first)                    push_back(*__first);            }        // Called by the second initialize_dispatch above        template<typename _ForwardIterator>            void            _M_range_initialize(_ForwardIterator __first,                    _ForwardIterator __last, forward_iterator_tag)            {                size_type __n = std::distance(__first, __last);                this->_M_impl._M_start = this->_M_allocate(__n);                this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;                this->_M_impl._M_finish = std::uninitialized_copy(__first, __last,                        this->_M_impl._M_start);            }        // Internal assign functions follow.  The *_aux functions do the actual        // assignment work for the range versions.        // Called by the range assign to implement [23.1.1]/9        template<typename _Integer>            void            _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)            {                _M_fill_assign(static_cast<size_type>(__n),                        static_cast<value_type>(__val));            }        // Called by the range assign to implement [23.1.1]/9        template<typename _InputIterator>            void            _M_assign_dispatch(_InputIterator __first, _InputIterator __last,                    __false_type)            {                typedef typename iterator_traits<_InputIterator>::iterator_category                    _IterCategory;                _M_assign_aux(__first, __last, _IterCategory());            }        // Called by the second assign_dispatch above        template<typename _InputIterator>            void            _M_assign_aux(_InputIterator __first, _InputIterator __last,                    input_iterator_tag);        // Called by the second assign_dispatch above        template<typename _ForwardIterator>            void            _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,                    forward_iterator_tag);        // Called by assign(n,t), and the range assign when it turns out        // to be the same thing.        void            _M_fill_assign(size_type __n, const value_type& __val);        // Internal insert functions follow.        // Called by the range insert to implement [23.1.1]/9        template<typename _Integer>            void            _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,                    __true_type)            {                _M_fill_insert(__pos, static_cast<size_type>(__n),                        static_cast<value_type>(__val));            }        // Called by the range insert to implement [23.1.1]/9        template<typename _InputIterator>            void            _M_insert_dispatch(iterator __pos, _InputIterator __first,                    _InputIterator __last, __false_type)            {                typedef typename iterator_traits<_InputIterator>::iterator_category                    _IterCategory;                _M_range_insert(__pos, __first, __last, _IterCategory());            }        // Called by the second insert_dispatch above        template<typename _InputIterator>            void            _M_range_insert(iterator __pos, _InputIterator __first,                    _InputIterator __last, input_iterator_tag);        // Called by the second insert_dispatch above        template<typename _ForwardIterator>            void            _M_range_insert(iterator __pos, _ForwardIterator __first,                    _ForwardIterator __last, forward_iterator_tag);        // Called by insert(p,n,x), and the range insert when it turns out to be        // the same thing.        void            _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);        // Called by insert(p,x)        void            _M_insert_aux(iterator __position, const value_type& __x);    };    /**     *  @brief  Vector equality comparison.     *  @param  x  A %vector.     *  @param  y  A %vector of the same type as @a x.     *  @return  True iff the size and elements of the vectors are equal.     *     *  This is an equivalence relation.  It is linear in the size of the     *  vectors.  Vectors are considered equivalent if their sizes are equal,     *  and if corresponding elements compare equal.     */    template<typename _Tp, typename _Alloc>        inline bool        operator==(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y)        {            return __x.size() == __y.size() &&                std::equal(__x.begin(), __x.end(), __y.begin());        }    /**     *  @brief  Vector ordering relation.     *  @param  x  A %vector.     *  @param  y  A %vector of the same type as @a x.     *  @return  True iff @a x is lexicographically less than @a y.     *     *  This is a total ordering relation.  It is linear in the size of the     *  vectors.  The elements must be comparable with @c <.     *     *  See std::lexicographical_compare() for how the determination is made.     */    template<typename _Tp, typename _Alloc>        inline bool        operator<(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y)        {            return std::lexicographical_compare(__x.begin(), __x.end(),                    __y.begin(), __y.end());        }    /// Based on operator==    template<typename _Tp, typename _Alloc>        inline bool        operator!=(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y)        { return !(__x == __y); }    /// Based on operator<    template<typename _Tp, typename _Alloc>        inline bool        operator>(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y)        { return __y < __x; }    /// Based on operator<    template<typename _Tp, typename _Alloc>        inline bool        operator<=(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y)        { return !(__y < __x); }    /// Based on operator<    template<typename _Tp, typename _Alloc>        inline bool        operator>=(const vector<_Tp,_Alloc>& __x, const vector<_Tp,_Alloc>& __y)        { return !(__x < __y); }    /// See std::vector::swap().    template<typename _Tp, typename _Alloc>        inline void        swap(vector<_Tp,_Alloc>& __x, vector<_Tp,_Alloc>& __y)        { __x.swap(__y); }} // namespace std#endif /* _VECTOR_H */

⌨️ 快捷键说明

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