永发信息网

模拟vector类中typedef T * iterator; iterator begin() { return iterator(p); } iterator(p)不懂?

答案:2  悬赏:0  手机版
解决时间 2021-02-05 08:27
那个return iterator(p) 不懂, iterator(p)怎么回事?是初始化或指针赋值么?
最佳答案
你好!

{ return const_reverse_iterator(begin()); } // [23.2.4.2] Called by assign(n,t), and the range assign when it turns out //

我的回答你还满意吗~~
全部回答
template<typename _Tp, typename _Alloc = allocator<_Tp> > class vector : protected _Vector_base<_Tp, _Alloc> { // Concept requirements. __glibcxx_class_requires(_Tp, _SGIAssignableConcept) typedef _Vector_base<_Tp, _Alloc> _Base; typedef vector<_Tp, _Alloc> vector_type; public: typedef _Tp value_type; typedef typename _Alloc::pointer pointer; typedef typename _Alloc::const_pointer const_pointer; typedef typename _Alloc::reference reference; typedef typename _Alloc::const_reference const_reference; typedef __gnu_cxx::__normal_iterator<pointer, vector_type> iterator; typedef __gnu_cxx::__normal_iterator<const_pointer, vector_type> const_iterator; typedef std::reverse_iterator<const_iterator> const_reverse_iterator; typedef std::reverse_iterator<iterator> reverse_iterator; typedef size_t size_type; typedef ptrdiff_t difference_type; typedef typename _Base::allocator_type allocator_type; protected: using _Base::_M_allocate; using _Base::_M_deallocate; using _Base::_M_impl; public: explicit vector(const allocator_type& __a = allocator_type()) : _Base(__a) explicit vector(size_type __n) : _Base(__n, allocator_type()) { this->_M_impl._M_finish = std::uninitialized_fill_n(this->_M_impl._M_start, __n, value_type()); } vector(const vector& __x) : _Base(__x.size(), __x.get_allocator()) { this->_M_impl._M_finish = std::uninitialized_copy(__x.begin(), __x.end(), this->_M_impl._M_start); } template<typename _InputIterator> vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a = allocator_type()) : _Base(__a) { // Check whether it's an integral type. If so, it's not an iterator. typedef typename _Is_integer<_InputIterator>::_Integral _Integral; _M_initialize_dispatch(__first, __last, _Integral()); } ~vector() vector& operator=(const vector& __x); void assign(size_type __n, const value_type& __val) template<typename _InputIterator> void assign(_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_assign_dispatch(__first, __last, _Integral()); } /// Get a copy of the memory allocation object. using _Base::get_allocator; iterator begin() const_iterator begin() const iterator end() const_iterator end() const reverse_iterator rbegin() const_reverse_iterator rbegin() const reverse_iterator rend() const_reverse_iterator rend() const // [23.2.4.2] capacity size_type size() const size_type max_size() const void resize(size_type __new_size, const value_type& __x) { if (__new_size < size()) erase(begin() + __new_size, end()); else insert(end(), __new_size - size(), __x); } void resize(size_type __new_size) size_type capacity() const bool empty() const void reserve(size_type __n); reference operator[](size_type __n) const_reference operator[](size_type __n) const protected: /// @if maint Safety check used only from at(). @endif void _M_range_check(size_type __n) const { if (__n >= this->size()) __throw_out_of_range(__N("vector::_M_range_check")); } public: reference at(size_type __n) const_reference at(size_type __n) const reference front() const_reference front() const reference back() const_reference back() const void push_back(const value_type& __x) { if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage) { std::_Construct(this->_M_impl._M_finish, __x); ++this->_M_impl._M_finish; } else _M_insert_aux(end(), __x); } void pop_back() { --this->_M_impl._M_finish; std::_Destroy(this->_M_impl._M_finish); } iterator insert(iterator __position, const value_type& __x); void insert(iterator __position, size_type __n, const value_type& __x) 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()); } iterator erase(iterator __position); iterator erase(iterator __first, iterator __last); 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); } void clear() protected: 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; } } 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); };
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
聚缘网吧怎么去啊,我要去那办事
涓报的意思是什么啊?知道的请说下!
邯郸哪个药房有卖安尔舒修复胶的
意中人网吧地址有知道的么?有点事想过去
八哥跟鹦鹉能关在一起吗??????
【初中化学实验操作视频】明天要考4个初中化
2016年10月初6早上八点出生的男孩叫什么名字
凌晨三点回家,一般是做什么工作的
什么是拉动经济增长的
腾达网吧地址在什么地方,我要处理点事
我有好几个网贷逾期了,逾期费特别高,现在越
刃铓的意思是什么啊?知道的请说下!
男士戴什么手串好呢,求推荐?
人的性染体色体存在于DA. 体细胞B. 生殖细胞C
论少年夫妻好,还是老夫少妻好
推荐资讯
为什么2的0.2次方会大于2的0次方
奥特曼传奇中小武的妈妈是谁扮演的?
罗技鼠标设置
岩峰窝地址在哪,我要去那里办事
盈佳电讯我想知道这个在什么地方
数据-5,3,2,-3,3的极差为A.8B.2C.6D.-8
板凳王重庆老灶火锅红星海店这个地址在什么地
7+()=47-()
【幸福的答案毛方圆】心安就是幸福的答案是什
杨家墩社区停车场地址有知道的么?有点事想过
骨董囊的意思是什么啊?知道的请说下!
有一个读作“谬”的数学符号,想U.是什么意识
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?