您的位置:首页 > 大数据 > 人工智能

Chapter 8 Visitation: Iterators and Containers

2008-03-25 19:17 351 查看
Chapter 8 Visitation: Iterators and Containers
tseraodnCin
• 8.1 Visitation
• 8.2 Iterators
• 8.3 An Example: quicksort()
• 8.4 Friendly Classes and Iterators
• 8.5 Generic Programming with void*
• 8.6 List and fist Iterator
• 8.7 Pragmatics
• Summary
• Exercises
Chapter 8 Visitation:
Iterators
tIseraotr
and Containers
tseraodnCin
• Container classes
C on t ai n e r c l as s e s
are used to hold a large
number of individual items.
• Many of the operations on container classes
involve the ability to visit individual
elements conveniently.
Chapter 8 Visitation:
Iterators
tIseraotr
and Containers
tseraodnCin
• In this chapter we explore a variety of
techniques to perform visitation and the
extraction of elements from a class. One
technique is to create an iterator whose
function is to visit the elements of an object
in a container class.
• Using visitation as the theme, we will write
code that anticipates using the container
classes and iterator objects and algorithms
found in the standard template library.
8.1 Visitation
• Conventional programming uses the for
statement as its preferred means of
structuring iteration, especially when
processing arrays.
8.1 Visitation
• Abstractly the computation is:
until no new elements
u n t i l n o n e w e l e m e n t s
sum += next element
n e xt e l e m e n t
• Demo on page 256: vector.h
– class vector
– boundaries of given vector
8.1 Visitation
8.1 Visitation
• Sample code
8.2 Iterators
• The pointer object and iterator visitation
idiom makes it very easy to write many
standard algorithms over container classes,
such as class vector.
• Demo on page 257: vector.h
– operator<<
– operator>>
8.2 Iterators
8.2 Iterators
• Demo on page 258: vectacum.cpp
– accumulate– sum the container values
8.3 An Example: quicksort()
• The quicksort
qu i c ks or t
sorting procedure, invented
by Anthony Hoare, is a highly efficient
internal sort.
• The qui cksort algorithm works by
recursively decomposing unordered values
into two subsets separated by a mid-value.
The mid-value is larger than all elements in
the first set and smaller than or equal to all
elements in the second set.
递归(Recursion)
-快速排序Quicksort
• 快速排序思路: (developed by C.A.R. Hoare in1962)
选中一个数组的元素,把数组分为两个子序
列,该元素前的序列中的元素都比它小,该
元素后的序列中的元素都比它大。以此类推
再将前后序列进行快速排序,直到序列中的
元素小于2个。
Demo qsort.c
递归
-Quicksort
• 快速排序思路: Idea of Quicksort
(1)swap(v,left,(left+right)/2);
left
(left+right)/2
right
递归
-Quicksort
• 快速排序思路: Idea of Quicksort
(2)partition
left
last
right
++last
4.10 递归
-Quicksort
• 快速排序思路: Idea of Quicksort
(3)swap(v,left,last);/*restore partion elem*/
left
last
right
4.10 递归
-Quicksort
• 快速排序思路: Idea of Quicksort
(3)qsort(v,left,last-1);(4)qsort(v,last+1,right)
left
last
right
last+1
last-1
8.3 An Example: quicksort()
• Demo on page 259: vectsort.cpp
– quicksort
– vector::iterator
v e c t o r : i t e r a t o r
partition
p a r t i t i o n
8.3 An Example: quicksort()
8.3 An Example: quicksort()
8.4 Friendly Classes and Iterators
• A class can define objects that are needed as
internal detail for other classes. A class that
handles these private objects must be on
friendly terms with them.
• Recall the mymystri
ng
type defined with
reference counting semantics in Section
6.10, "Strings Using Reference Semantics,"
on page 197.
– my_string
– str_obj
8.4 Friendly Classes and Iterators
• Demo on page 262: string8.h
– class str_obj
8.4 Friendly Classes and Iterators
• Iterators also typically need friendly
relations to the object they are visiting. Let
us add an iterator class and modify our
my_string to overload assignment and the
put to operator filling out this example.
• Demo on page 263: string8.h
– class my_string
– operator<<
8.4 Friendly Classes and Iterators
8.4 Friendly Classes and Iterators
– ostream& operator<<(ostream&, const my_
string&) requires friendly relations to str_obj.
– It cannot
c a n o t
be written as a member function
because its first argument must be an ostream.
– Since its return value is an ostream&, it can be
used in a multiple put to expression.
– It is worth pointing out that were not
assignment overloaded, the default assignment
semantics would fail.
8.4 Friendly Classes and Iterators
– We keep a private position variable cur_ind.
– Since the friendship relation between my_string
and str_ob j is not transitive,
• Demo on page 264: string8.h
– string_iterator, successor() member function
8.4 Friendly Classes and Iterators
8.4 Friendly Classes and Iterators
• Demo on page 264: string8.cpp
– word
8.5 Generic Programming with
void*
• The pointer type void* serves as a generic
or universal pointer type. Any other pointer
type can be assigned to it.
• We can see this in the definition of the
standard memory copying function memcpy.
• Demo on page 265: memcopy.cpp
8.5 Generic Programming with
void*
8.5 Generic Programming with
void*
• Demo on page 266: genstack.h
– class stack
8.5 Generic Programming with
void*
• Demo on page 266: month.cpp
8.6 List and fist Iterator
• In this section we develop a doubly linked
list whose interface is similar to the STL
library type list.
• We will give a basic design of this class and
its associated iterator definitions.
• Demo on page 267: list2.h
8.6 List and fist Iterator
8.6 List and fist Iterator
– We have struct 1istelem, which has a data
member and pointers to the next and previous
list elements.
– In our design, we add an iterator class nested
inside the list class. This class list::iterator will
be used to point at a current position inside a
list.
8.6 List and fist Iterator
• Demo on page 268: list2.h
– class iterator
8.6 List and fist Iterator
• Demo on page 268: list2.h
– operator++
– operator<<
8.6 List and fist Iterator
• Demo on page 268: list2.h
– The apparent dereferencing of p is
accomplished by accessing the data member of
the appropriate 1istelem using
1ist::iterator::operator*().
– The STL member functions for list are very
extensive; we will only show the code for a
representative set and will place several more in
the exercises.
8.6 List and fist Iterator
• Demo on page 268: list2.h
– push_front(char c)
8.6 List and fist Iterator
• Demo on page 268: list2.h
– list constructors
– The default constructor producing an empty list
has already been discussed. The second
constructor produces a list of n elements, all
initialized with a data member passed in as c.
8.6 List and fist Iterator
• Demo on page 268: list2.h
– list constructors
8.6 List and fist Iterator
• Demo on page 268: list2.h
– list destructor
8.7 Pragmatics
• Notice how the class list::iterator
mimics
definitions that are appropriate for post- and
pre- autoincrement and autodecrement.
• Demo on page list2.h:
– post- and pre- increment and decrement
8.7 Pragmatics
Summary
• 1. Visitation of the elements of an aggregate is a
fundamental operation.
• 2. Containers should designate a begin()
and an
end()
member returning an iterator.
• 3.
The pointer type void
*
serves as a generic or
universal pointer type.
• 4. We developed a doubly linked list whose
interface is similar to the STL library type list.
• 5. Iterators are either pointers or pointer-like
objects. 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息