Chapter 6

advertisement
Chapter 6
– The List Container and Iterators
Shifting blocks of elements…
Model of a list object…
Sample list
The list ADT
CLASS list Constructors
CLASS list Operations (7 slides)
CLASS list::iterator Operations
Inserting an element into a list
Removing an element from a list
Ordered lists
Splicing two lists
Summary Slides (5 slides)
1
Main Index
Contents
Shifting blocks of elements to
insert or delete a vector item
2
S hift right
Initial Vec to r
Ins ert 25 at
15 20 30 35 40
P o s itio n 2
0 1 2 3 4
15 20
Initial Vec to r
Eras e 20 at
15 20 30 35 40
P o s itio n 1
0 1 2 3 4
15
Main Index
Contents
0
0
1
25
2
30 35 40
3
S hift left
30 35 40
20
1
2 3
4
5
Model of a list object with links
to next and previous element
fro nt
3
b ac k
Main Index
Contents
Sample list
P a s ta D e le c tic a
6:00
J ones (4)
Liu (2)
Bouton (6)
6:15
F ranks (3)
G onzales (5)
4
Main Index
Contents

The List ADT
The list API documents the member function
prototype as well as pre- and postconditions.
–
provides three constructors to declare a list object.
(a) lis t< d o ub le> realLis t(8)
0 .0
0 .0
0 .0
0 .0
0 .0
0 .0
(b ) lis t< tim e24> tim eLis t(6, 8:30)
8 :3 0
8 :3 0
8 :3 0
8 :3 0
8 :3 0
8 :3 0
(c ) lis t< s tring> s trLis t(s trA rr, s trA rr+ 3)
array
5
ve c to r
Main Index
lis t
Contents
0 .0
0 .0
CLASS list
Constructors
<list>
list();
Create an empty list. This is the default constructor.
list(int n, const T&value = T());
Create a list with n elements, each having a specified
value. If the value argument is omitted, the elements
are filled with the default value for type T. Type T must
have a default constructor, and the default value of
type T is specified by the notation T().
list(T *first, T *last);
Initialize the list, using the address range [first, last).
6
Main Index
Contents
CLASS list
Operations
<list>
T& back();
Return the value of the item at the rear of the list.
Precondition:
The vector must contain at least one
element.
bool empty() const;
Return true if the vector is empty, false otherwise.
T& front();
Return the value of the item at the front of the list.
Precondition:
The vector must contain at least one
element.
7
Main Index
Contents
CLASS list
Operations
<list>
void push_back(const T& value);
Add a value at the rear of the list.
Postcondition: The list has a new element at the
rear, and its size increases by 1.
void pop_back();
Remove the item at the rear of the list.
Precondition:
The list is not empty.
Postcondition: The list has a new element at the rear
or is empty.
8
Main Index
Contents
CLASS list
Operations
<list>
void push_front(const T& value);
Add a value at the front of the list.
Postcondition: The list has a new element at the
front, and its size increases by 1.
void pop_front();
Remove the item at the front of the list.
Precondition:
The list is not empty.
Postcondition: The list has a new element at the front
or is empty.
int size() const;
Return the number of elements in the vector.
9
Main Index
Contents
CLASS list
Operations
<list>
iterator begin();
Returns an iterator that references the first position
(front) of the list. If the list is empty, the iterator value
end() is returned.
const_iterator begin();
Returns a const_iterator that points to the first position
(front) of a constant list. If the list is empty, the
const_iterator value end() is returned.
iterator end();
Returns an iterator that signifies a location immediately
out of the range of actual elements. A program must
not dereference the value of end() with the * operator.
10
Main Index
Contents
CLASS list
Operations
<list>
iterator end();
Returns an iterator that signifies a location immediately
out of the range of actual elements. A program must
not dereference the value of end() with the * operator.
const_iterator end();
Returns a const_iterator that signifies a location
immediately out of the range of actual elements in a
constant list. A program must not dereference the
value of end() with the * operator.
11
Main Index
Contents
CLASS list
Operations
<list>
void erase(iterator pos);
Erase the element pointed to by pos.
Precondition:
The list is not empty.
Postcondition: The list has one fewer element.
void erase(iterator first, iterator last);
Erase all list elements within the iterator range [first,
last].
Precondition:
The list is not empty.
Postcondition: The size of the list decreases by the
number of elements in the range.
12
Main Index
Contents
CLASS list
Operations
<list>
iterator insert(iterator pos, const T& value);
Insert value before pos, and return an iterator pointing
to the position of the new value in the list. The
operation does not affect any existing iterators.
Postcondition: The list has a new element.
13
Main Index
Contents
CLASS list::iterator
Operations
<list>
*:Accesses the value of the item currently pointed to by the
iterator.
*iter;
++: Moves the iterator to the next item in the list.
iter++;
--:
Moves the iterator to the previous item in the list.
iter--;
==: Takes two iterators as operands and returns truewhen they
both point at the same item in the list.
iter1 == iter2
!=:
14
Returns true when the two iterators do not point at the same
item in the list.
iter1 != iter2
Main Index
Contents
Inserting an element into a list
List object (before)
2
front
15
7
iter
3
9
List object (after)
5
rear
4
Main Index
2
4
7
front
newElt iter
Contents
3
9
5
rear
Removing an element from a list
List object (before)
2
7
front
3
9
List object (after)
5
2
rear
front
iter
16
Main Index
Contents
3
9
5
rear
??
iter
Ordered lists
60
65
74
82
rear
front
Position the iterator curr at the front of
the list.
Insert 50 in the list:
Before Insert
front
50
17
60
65
74
A fter Insert
82
50
rear
front
curr
Main Index
Contents
60
65
74
82
rear
Splicing two lists
destList
7
3
sourceList pos
15 16
sourceIter
18
4
destList (A fter insert of 15) destList (A fter insert of 16)
5
7 15 3
4
5
sourceList pos
15 16
sourceList
15 16
sourceIter
Main Index
7 15 16 3
pos
sourceIter
Contents
4
5
Summary Slide 1
§- list
- A Sequence of elements stored by position.
- Index access is not available…
§- to access the value of an element, must pass
through its preceding elements.
§- list iterator
- A generalized pointer that moves through a list
element by element… forward or backward
- At any point, the * operator accesses the value of
a list item.
19
Main Index
Contents
Summary Slide 2
§- The list class has two iterator types:
1)iterator:
A generalized list traversal pointer.
2)const _ iterator:
must be used with a constant list object. Each
type is a nested class of list and must be
accessed by using the scope operator ::
20
Main Index
Contents
Summary Slide 3
§- the list member function begin()
- Gives an iterator an initial value that points to the
first element.
§- the list member function end()
- Returns an iterator pointing just past the last
element of the list.
21
Main Index
Contents
Summary Slide 4
§- The sequential search of a list object
- implemented by using an iterator range
[first, last).
- It returns an iterator that points at the target value
or has value last if the target is not in the list.
22
Main Index
Contents
Summary Slide 5
§- list class member fns insert() and erase()
- Both use an iterator argument to modify a list.
1) insert():
places value in the list before the data
referenced by the iterator pos.
2) erase():
removes the data item referenced by pos from the
list.
23
Main Index
Contents
Download