📄 stl_vector.h
字号:
_M_finish = uninitialized_fill_n(_M_start, __n, __value); } template<class _InputIterator> void _M_initialize_aux(_InputIterator __first, _InputIterator __last, __false_type) { typedef typename iterator_traits<_InputIterator>::iterator_category _IterCategory; _M_range_initialize(__first, __last, _IterCategory()); } ~vector() { _Destroy(_M_start, _M_finish); } vector<_Tp, _Alloc>& operator=(const vector<_Tp, _Alloc>& __x); /** * @brief Attempt to preallocate enough memory for specified number of * elements. * @param n Number of elements required * * This function attempts to reserve enough memory for the vector to hold * the specified number of elements. If the number requested is more than * max_size() length_error is thrown. * * The advantage of this function is that if optimal code is a necessity * and the user can determine the number of elements that will be required * the user can reserve the memory and thus prevent a possible * reallocation of memory and copy of vector data. */ void reserve(size_type __n) { if (capacity() < __n) { const size_type __old_size = size(); pointer __tmp = _M_allocate_and_copy(__n, _M_start, _M_finish); _Destroy(_M_start, _M_finish); _M_deallocate(_M_start, _M_end_of_storage - _M_start); _M_start = __tmp; _M_finish = __tmp + __old_size; _M_end_of_storage = _M_start + __n; } } // assign(), a generalized assignment member function. Two // versions: one that takes a count, and one that takes a range. // The range version is a member template, so we dispatch on whether // or not the type is an integer. /** * @brief Assigns a given value or range to a vector. * @param n Number of elements to be assigned. * @param val Value to be assigned. * * This function can be used to assign a range to a vector or fill it * with a specified number of copies of the given value. * Note that the assignment completely changes the vector and that the * resulting vector's size is the same as the number of elements assigned. * Old data may be lost. */ void assign(size_type __n, const _Tp& __val) { _M_fill_assign(__n, __val); } void _M_fill_assign(size_type __n, const _Tp& __val); template<class _InputIterator> void assign(_InputIterator __first, _InputIterator __last) { typedef typename _Is_integer<_InputIterator>::_Integral _Integral; _M_assign_dispatch(__first, __last, _Integral()); } template<class _Integer> void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type) { _M_fill_assign((size_type) __n, (_Tp) __val); } template<class _InputIter> void _M_assign_dispatch(_InputIter __first, _InputIter __last, __false_type) { typedef typename iterator_traits<_InputIter>::iterator_category _IterCategory; _M_assign_aux(__first, __last, _IterCategory()); } template <class _InputIterator> void _M_assign_aux(_InputIterator __first, _InputIterator __last, input_iterator_tag); template <class _ForwardIterator> void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last, forward_iterator_tag); /** * Returns a read/write reference to the data at the first element of the * vector. */ reference front() { return *begin(); } /** * Returns a read-only (constant) reference to the data at the first * element of the vector. */ const_reference front() const { return *begin(); } /** * Returns a read/write reference to the data at the last element of the * vector. */ reference back() { return *(end() - 1); } /** * Returns a read-only (constant) reference to the data at the first * element of the vector. */ const_reference back() const { return *(end() - 1); } /** * @brief Add data to the end of the vector. * @param x Data to be added. * * This is a typical stack operation. The function creates an element at * the end of the vector and assigns the given data to it. * Due to the nature of a vector this operation can be done in constant * time if the vector has preallocated space available. */ void push_back(const _Tp& __x) { if (_M_finish != _M_end_of_storage) { _Construct(_M_finish, __x); ++_M_finish; } else _M_insert_aux(end(), __x); }#ifdef _GLIBCPP_DEPRECATED /** * Add an element to the end of the vector. The element is * default-constructed. * * @note You must define _GLIBCPP_DEPRECATED to make this visible; see * c++config.h. */ void push_back() { if (_M_finish != _M_end_of_storage) { _Construct(_M_finish); ++_M_finish; } else _M_insert_aux(end()); }#endif void swap(vector<_Tp, _Alloc>& __x) { std::swap(_M_start, __x._M_start); std::swap(_M_finish, __x._M_finish); std::swap(_M_end_of_storage, __x._M_end_of_storage); } /** * @brief Inserts given value into vector at specified element. * @param position An iterator that points to the element where data * should be inserted. * @param x Data to be inserted. * @return An iterator that points to the inserted data. * * This function will insert the given value into the specified location. * 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. */ iterator insert(iterator __position, const _Tp& __x) { size_type __n = __position - begin(); if (_M_finish != _M_end_of_storage && __position == end()) { _Construct(_M_finish, __x); ++_M_finish; } else _M_insert_aux(iterator(__position), __x); return begin() + __n; } /** * @brief Inserts an empty element into the vector. * @param position An iterator that points to the element where empty * element should be inserted. * @param x Data to be inserted. * @return An iterator that points to the inserted element. * * This function will insert an empty element into the specified location. * 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. */ iterator insert(iterator __position) { size_type __n = __position - begin(); if (_M_finish != _M_end_of_storage && __position == end()) { _Construct(_M_finish); ++_M_finish; } else _M_insert_aux(iterator(__position)); return begin() + __n; } // Check whether it's an integral type. If so, it's not an iterator. template<class _InputIterator> void insert(iterator __pos, _InputIterator __first, _InputIterator __last) { typedef typename _Is_integer<_InputIterator>::_Integral _Integral; _M_insert_dispatch(__pos, __first, __last, _Integral()); } template <class _Integer> void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val, __true_type) { _M_fill_insert(__pos, static_cast<size_type>(__n), static_cast<_Tp>(__val)); } template<class _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()); } /** * @brief Inserts a number of copies of given data into the vector. * @param position An iterator that points to the element where data * should be inserted. * @param n Amount of elements to be inserted. * @param x Data to be inserted. * * This function will insert a specified number of copies of the given data * into the specified location. * * 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. */ void insert (iterator __pos, size_type __n, const _Tp& __x) { _M_fill_insert(__pos, __n, __x); } void _M_fill_insert (iterator __pos, size_type __n, const _Tp& __x); /** * @brief Removes last element from vector. * * This is a typical stack operation. It allows us to shrink the vector by * one. * * Note that no data is returned and if last element's data is needed it * should be retrieved before pop_back() is called. */ void pop_back() { --_M_finish; _Destroy(_M_finish); } /** * @brief Remove element at given position * @param position Iterator pointing to element to be erased. * @return Doc Me! (Iterator pointing to new element at old location?) * * 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) { if (__position + 1 != end()) copy(__position + 1, end(), __position); --_M_finish; _Destroy(_M_finish); return __position; } /** * @brief Remove a range of elements from a vector. * @param first Iterator pointing to the first element to be erased. * @param last Iterator pointing to the last element to be erased. * @return Doc Me! (Iterator pointing to new element at old location?) * * This function will erase the elements in the given range 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) { iterator __i(copy(__last, end(), __first)); _Destroy(__i, end()); _M_finish = _M_finish - (__last - __first); return __first; } /** * @brief Resizes the vector to the specified number of elements. * @param new_size Number of elements the vector should contain. * @param x Data with which new elements should be populated. * * This function will resize the vector to the specified number of * elements. If the number is smaller than the vector's current size the * vector is truncated, otherwise the vector is extended and new elements * are populated with given data. */ void resize(size_type __new_size, const _Tp& __x) { if (__new_size < size()) erase(begin() + __new_size, end()); else insert(end(), __new_size - size(), __x); } /** * @brief Resizes the vector to the specified number of elements. * @param new_size Number of elements the vector should contain. * * This function will resize the vector to the specified number of * elements. If the number is smaller than the vector's current size the * vector is truncated, otherwise the vector is extended and new elements * are left uninitialized. */ void resize(size_type __new_size) { resize(__new_size, _Tp()); } /** * Erases all elements in vector. 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: template <class _ForwardIterator> pointer _M_allocate_and_copy(size_type __n, _ForwardIterator __first, _ForwardIterator __last) { pointer __result = _M_allocate(__n); try { uninitialized_copy(__first, __last, __result); return __result; } catch(...) { _M_deallocate(__result, __n);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -