The List Container and Iterators - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

The List Container and Iterators

Description:

bool operator (const iterator& iter) const; iterator begin(); Nested (embedded) Iterator Class ... iter; : Moves the iterator to the next item in the list. iter ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 26
Provided by: jian9
Category:

less

Transcript and Presenter's Notes

Title: The List Container and Iterators


1
The List Container and Iterators
Chapter 6
2
Outline
  • List Container
  • list vs. vector
  • list objects
  • The list class
  • Constructors
  • Operations
  • Iterators
  • listiterator Operations
  • Inserting an element into a list
  • Removing an element from a list
  • Ordered lists

3
Insert or Delete a vector Item
Shifting blocks of elements
4
A list object with links to next and previous
element.
List container and object
  • A list is 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.

5
The list class - Declare a list object
  • list is a template class

// create an empty list that stores
integers listltintgt intList
// create an empty list that stores
Employees listltEmployeegt empList
// create a list with 8 elements listltdoublegt
doubleList(8)
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
// a list of 6 time24 objects with initial value
830 listlttime24gt timeList(6, time24(8, 30))
830
830
830
830
830
830
6
list() Create an empty list. This is the
default constructor.
list(int n, const Tvalue 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().
7
T back() Return the value of the item at the
rear of the list. Precondition The vector
must contain at least one element.
T front() Return the value of the item at the
front of the list. Precondition The vector
must contain at least one element.
8
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.
9
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.
10
int size() const Return the number of elements
in the vector.
bool empty() const Return true if the vector is
empty, false otherwise.
11
intList.push_front(5) intList.push_front(20)
intList.push_back(10) intList.push_back(25)
value intList.front() intList.back() 15
intList.pop_front() intList.pop_back()
intList.pop_back()
20
5
10
25
front
back
20
5
10
15
front
back
5
10
15
back
front
5
10
front
back
5
front
back
12
Iterators
  • An iterator is an object that enables a user to
    loop through a container
  • The list class includes an iterator class as a
    nested class with the ability to do at least the
    following
  • Start at the beginning of the list container
  • Determine when no more loop iterations are
    possible, e.g., at the end
  • Advance to the next item in the list container
  • Return the item where the iterator is now
    positioned.
  • list iterator is 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.

13
Nested (embedded) Iterator Class
  • templateltclass Tgt
  • class list
  • private public class iterator friend
    class listltTgt
  • private
  • public
  • T operator() const
  • bool operator(const iterator iter)
    const
  • iterator begin()

14
Declaring Iterators
  • The declaration of an iterator object must
    include the class name iterator along with the
    scope operator expression listltTgt, which
    identifies the iterator as belonging to the list
    class.
  • listltintgtiterator intListIter
  • listltstringgtiterator strListIter
  • listltEmployeegtiterator empListIter

15
20
5
10
25
Begin()
end()
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.
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.
intListIter intList.begin()
20
5
10
25
intListIter
16
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.
17
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.
18
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
! Returns true when the two iterators do not
point at the same item in the list. iter1 !
iter2
19
20
10
10
25
iterB
iterA
cout ltlt iterA // output 20 iterA //
moves to next position (item 10)
iterA iterB // false they point to
different items iterA iterB // true values
referenced are equal
20
10
10
25
iterA
iterB
iterA // moves to next position (item
10) iterA iterB // true they point to the
same item
20
10
10
25
iterA
iterB
20
List iterator Example
  • A user can iterate through a list object
  • listltstringgt sneakerList
  • sneakerList.push_front(Adidas)
  • sneakerList.push_front(Nike)
  • sneakerList.push_back(Reebok)
  • Listltstringgtiterator iterfor (iter
    sneakerList.begin() iter ! sneakerList.end()
    iter)
  • if (iter Adidas) cout ltlt Found
    Adidas! if ((iter).length() 4)
    cout ltlt Found a 4-letter words.

Nike
Adidas
Reebok
iter
21
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.

Example 6-3
22
Inserting an element into a list
listltintgtiterator iter, newElt iterintList.be
gin() iter newIterintList.insert(iter,
4) cout ltlt newIter ltlt ltlt iter
  • Note the original iterator continues to point
    at the same element.

23
Removing an element from a list
iterintList.begin() iter intList.erase(iter)
  • The operation destroys the element, so the
    iterator becomes invalid.

24
Ordered lists
// Position the iterator curr at the front of the
list. curr intList.begin() // Insert 50 in the
list intList.insert(curr, 50)
25
Ordered lists
  • template lttypename Tgt
  • void insertOrder(listltTgt orderedList, const T
    item)
  • // curr starts at first list element, stop
    marks end
  • listltTgtiterator curr orderedList.begin(),
  • stop orderedList.end()
  • // find the insertion point, which may be at
    end of list
  • while ((curr ! stop) (curr lt item))
  • curr
  • // do the insertion using insert()
  • orderedList.insert(curr, item)
Write a Comment
User Comments (0)
About PowerShow.com