Shifting blocks of elements - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Shifting blocks of elements

Description:

Inserting an element into a list. Removing an element from a ... multimap. Container adapters (no iterators supported) stack. queue. priority_queue. Iterator ... – PowerPoint PPT presentation

Number of Views:57
Avg rating:3.0/5.0
Slides: 25
Provided by: alaneu
Category:

less

Transcript and Presenter's Notes

Title: Shifting blocks of elements


1
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 listiterator Operations Inserting
an element into a list Removing an element from a
list Ordered lists Splicing two lists Summary
Slides (5 slides)
2
Shifting blocks of elements to insert or delete a
vector item
3
Model of a list object with links to next and
previous element
4
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.

5
(No Transcript)
6
(No Transcript)
7
(No Transcript)
8
(No Transcript)
9
(No Transcript)
10
(No Transcript)
11
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.
12
21.1.2 Introduction to Iterators
  • Iterators similar to pointers
  • Point to first element in a container
  • Iterator operators same for all containers
  • dereferences
  • points to next element
  • begin() returns iterator to first element
  • end() returns iterator to last element
  • Use iterators with sequences (ranges)
  • Containers
  • Input sequences istream_iterator
  • Output sequences ostream_iterator

13
21.1.2 Introduction to Iterators
  • Usage
  • stdistream_iteratorlt int gt inputInt( cin )
  • Can read input from cin
  • inputInt
  • Dereference to read first int from cin
  • inputInt
  • Go to next int in stream
  • stdostream_iteratorlt int gt outputInt(cout)
  • Can output ints to cout
  • outputInt 7
  • Outputs 7 to cout
  • outputInt
  • Advances iterator so we can output next int

14
fig21_05.cpp(1 of 2)
  • 1 // Fig. 21.5 fig21_05.cpp
  • 2 // Demonstrating input and output with
    iterators.
  • 3 include ltiostreamgt
  • 4
  • 5 using stdcout
  • 6 using stdcin
  • 7 using stdendl
  • 8
  • 9 include ltiteratorgt // ostream_iterator
    and istream_iterator
  • 10
  • 11 int main()
  • 12
  • 13 cout ltlt "Enter two integers "
  • 14
  • 15 // create istream_iterator for reading
    int values from cin
  • 16 stdistream_iteratorlt int gt inputInt(
    cin )
  • 17
  • 18 int number1 inputInt // read int
    from standard input
  • 19 inputInt // move iterator to
    next input value

22 // create ostream_iterator for writing
int values to cout 23 stdostream_iteratorlt
int gt outputInt( cout ) 24 25
cout ltlt "The sum is " 26 outputInt
number1 number2 // output result to cout 27
cout ltlt endl 28 29 return 0 30
31 // end main
15
fig21_05.cpp(2 of 2)fig21_05.cppoutput (1 of
1)
Enter two integers 12 25 The sum is 37
16
Iterator Categories
  • Input
  • Read elements from container, can only move
    forward
  • Output
  • Write elements to container, only forward
  • Forward
  • Combines input and output, retains position
  • Multi-pass (can pass through sequence twice)
  • Bidirectional
  • Like forward, but can move backwards as well
  • Random access
  • Like bidirectional, but can also jump to any
    element

17
Iterator Types Supported
  • Sequence containers
  • vector random access
  • deque random access
  • list bidirectional
  • Associative containers (all bidirectional)
  • set
  • multiset
  • Map
  • multimap
  • Container adapters (no iterators supported)
  • stack
  • queue
  • priority_queue

18
Iterator
  • 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.
  • - 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.

19
(No Transcript)
20
(No Transcript)
21
Inserting an element into a list
22
Removing an element from a list
23
Ordered lists
Position the iterator curr at the front of the
list. Insert 50 in the list
24
Splicing two lists
Write a Comment
User Comments (0)
About PowerShow.com