{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Educn reservenv vsizen struct link link next linklink

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (迭代器) typedef implementation_dependent2const_ iterator; // const T* typedef std::reverse_iterator<iterator> reverse_iterator; typedef std::reverse_iterator<const_iterator> // 反向迭代器 const_reverse_iterator; typedef typename A::pointer pointer; // 元素指针 typedef typename A::const_pointer const_pointer; typedef typename A::reference reference; // 元素引用 typedef typename A::const_reference const_reference; }; Beijing University of Posts & Telecommunications Beijing [email protected] 类型 使用容器的代码 必须在作为模板参数的成员类型名字之前加 typename,不加的话,编译器无法判断 template<class C> typename C::value_type sum(const C& c) { typename C::value_type s = 0; typename C::const_iterator p = c.begin() ; // 从头开始 while (p!=c.end()) { // 继续到结束处 s += *p; // 取得元素的数值 ++p; // 使p指向下一个元素 } return s; } Beijing University of Posts & Telecommunications Beijing [email protected] 16.3.2 迭代器 template <class T, class A = allocator<T> > class vector { public: iterator begin() ; // 指向首元素 const_iterator begin() const; iterator end() ; // 指向末端元素的下一个位置 const_iterator end() const; reverse_iterator rbegin() ; // 指向反向序列的首元素 const_reverse_iterator rbegin() const; reverse_iterator rend() ; // 同上(end()) element of reverse sequence const_reverse_iterator rend() const; // ... }; Beijing University of Posts & Telecommunications Beijing [email protected] 迭代器 template<class C> typename C::iterator find_last(const C& c, typename C::value_type v) { // 对vector进行反向搜索,找到第一个出现v的位...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online