Types of Containers: Sequences - Kent State...

28
Types of Containers: Sequences

Transcript of Types of Containers: Sequences - Kent State...

Page 1: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

Types of Containers: Sequences

Page 2: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

Bag vs. Sequence

Both contain items Sequence: items are arranged in sequence

and order matters Bag: items are arranged in sequence but

order does not matter See deletion operation

Page 3: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

Internal Iterators

Iterator: An object that permits the programmer to traverse a container

Internal Iterator: a technique to use member functions of a container to traverse the container

External Iterator: objects offered by the Standard Library to traverse a container.

Page 4: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

Sequence

typedef ____ value_type sequence::value_type is the data type of the items in the sequence. It may

be any of the C++ built-in types (int, char, etc.), or a class with a default constructor, an assignment operator, and a copy constructor.

typedef ____ size_type sequence::size_type is the data type of any variable that keeps track of how

many items are in a sequence. static const size_type CAPACITY = _____ the maximum number of items that a sequence can hold. CONSTRUCTOR for the sequence class: sequence( ) Postcondition: The sequence has been initialized as an empty sequence.

Page 5: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

Modification member functions

void start( ) /* Postcondition: The first item on the sequence

becomes the current item (but if the sequence is empty, then there is no current item). */

void advance( ) // Precondition: is_item returns true. /* Postcondition: If the current item was already the last

item in the sequence, then there is no longer any current item. Otherwise, the new current item is the item immediately after the original current item. */

Page 6: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

Modification member functions

void insert(const value_type& entry)

// Precondition: size( ) < CAPACITY. /* Postcondition: A new copy of entry has been inserted in the

sequence before the current item. If there was no current item, then the new entry has been inserted at the front of the sequence. In either case, the newly inserted item is now the current item of the sequence.*/

To maintain the order - (Pseudocode implementation) for (i= used; i>index_current_item; ; ++i) data[i] = data[i-1]

Page 7: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

An Example of Calling Insert in Bag

void bag::insert(int new_entry)

b.data

b.count

We make a function call b.insert(17) at position i

What values will be in b.data and b.count after the member function finishes ?

2

[ 0 ] [ 1 ] [2] . . .

8 4

void bag::insert(int new_entry)

Presenter
Presentation Notes
. . . and we call bag::insert to insert the number 17. What will be the values of b.data and b.count after the insertion?
Page 8: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

An Example of Calling Insert in Bag

void bag::insert(int new_entry)

After calling b.insert(17), we will have this bag b:

3

[ 0 ] [1] [ 2 ] . . .

8 4 17

void bag::insert(int new_entry)

b.data

b.count 2

[ 0 ] [ 1 ] [2] . . .

8 4

Presenter
Presentation Notes
We have added the new number, 17, to the next spot in the array; and we have incremented b.count by one to indicate that we are now using one more spot in the array.
Page 9: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

An Example of Calling Insert in Sequence

void bag::insert(int new_entry)

seq.data

seq.count

We make a function call seq.insert(17) current_index=1

What values will be in seq.data and seq.count after the member function finishes ?

2

[ 0 ] [ 1 ] [2] . . .

8 4

void sequence::insert(int new_entry, )

Presenter
Presentation Notes
. . . and we call bag::insert to insert the number 17. What will be the values of b.data and b.count after the insertion?
Page 10: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

An Example of Calling Insert in Bag

void bag::insert(int new_entry)

After calling seq.insert(17), we will have this bag b:

3

[ 0 ] [1] [ 2 ] . . .

8 17 4

void sequence::insert(int new_entry)

seq.data

seq.count 2

[ 0 ] [ 1 ] [2] . . .

8 4

Presenter
Presentation Notes
We have added the new number, 17, to the next spot in the array; and we have incremented b.count by one to indicate that we are now using one more spot in the array.
Page 11: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

Modification member functions

void remove_current( )

// Precondition: is_item returns true. /* Postcondition: The current item has been removed from the

sequence, and the item after this (if there is one) is now the new current item.*/

What is the pseudocode?

Page 12: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

Modification member functions

void attach(const value_type& entry)

// Precondition: size( ) < CAPACITY. /* Postcondition: A new copy of entry has been

inserted in the sequence after the current item. If there was no current item, then the new entry has been attached to the end of the sequence. In either case, the newly inserted item is now the current item of the sequence.*/

Page 13: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

Set and multiset

The set class stores elements without repetition

The multiset class stores elements with repetition and strict weak ordering (for efficient implementation) Strict weak ordering (<)

Irreflexivity: if x=y, then neither (x<y) nor (y<x) is true ( x<x is never true)

Antisymmetry: if x and y are not equal, then either (x<y) or (y<x) is true, but not both.

Transitivity: Whenever there are 3 values (x, y, and z) with (x<y) and (y<z0, then (x< z) is also true.

Page 14: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

Multiset members similar to Bag

multiset<int> first; first.insert(8); first.insert(4); first.insert(8); The multiset will contain two 8 and one 4 in the order inserted. size_type count(const value_type& target)

const; size_type erasevalue_type& target); size_type size();

Page 15: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

Insert Member function

iterator insert(const value_type& entry); STL member functions begin() // returns an iterator providing access to the first

item in the container multiset<string>::iterator role; role = actors.begin();

Page 16: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

The * operator

Once a program has created an iterator, the *(asterisk) operator can be used to access the current element.

cout << *role << endl;

Attention: the iterator is NOT a pointer, but the *

operator is used (similarly to a pointer) to get the item in the current position.

Page 17: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

The ++ operator

Once a program has created an iterator, the ++ operator can be used to move the iterator forward. ++role; //returns the first iterator after it has already moved forward

Or role++; // returns the iterator before it has moved forward

Page 18: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

Insert Member function

More STL member functions end()

// returns an iterator to mark the end of an item. Once it reached the end it has already gone beyond the last item of the container and the * operator must not be used anymore because there are no items.

Page 19: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

Examining a sequence

for (i=c.begin(); i!=c.end(); ++i)

{

..statements to access the item *i

}

Left-Inclusive Pattern […): The for loop iterates through a set of values at begin() and going up (but not including) the end() value

Page 20: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

Changing the container

multiset<int> m;

multiset<int>:: iterator position;

position = m.find(24);

if (position != m.end())

m.erase(position)

ATTENTION: There are cases when changing a container object causes the invalidation of its iterators.

Example: the position iterator is invalid after its item is erased

Page 21: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

An Example of Calling Insert

void bag::insert(int new_entry)

b.data

b.count

We make a function call b.insert(17)

What values will be in b.data and b.count after the member function finishes ?

2

[ 0 ] [ 1 ] [2] . . .

8 4

void bag::insert(int new_entry)

Presenter
Presentation Notes
. . . and we call bag::insert to insert the number 17. What will be the values of b.data and b.count after the insertion?
Page 22: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

An Example of Calling Insert

void bag::insert(int new_entry)

After calling b.insert(17), we will have this bag b:

3

[ 0 ] [1] [ 2 ] . . .

8 4 17

void bag::insert(int new_entry)

b.data

b.count 2

[ 0 ] [ 1 ] [2] . . .

8 4

Presenter
Presentation Notes
We have added the new number, 17, to the next spot in the array; and we have incremented b.count by one to indicate that we are now using one more spot in the array.
Page 23: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

Pseudocode for bag::insert

assert(size( ) < CAPACITY); Place new_entry in the appropriate location

of the data array. Add one to the member variable count.

data[count] = new_entry; count++;

Presenter
Presentation Notes
Here is one solution for Steps 2 and 3. In our example, this would cause the new_entry (17) to be placed at index [2] of the array b.data, and then count is incremented to 3.
Page 24: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

Pseudocode for bag::insert

assert(size( ) < CAPACITY); Place new_entry in the appropriate location

of the data array. Add one to the member variable count.

data[ count++] = new_entry;

Presenter
Presentation Notes
Here is an alternative that combines Steps 2 and 3. In this alternative, the index [count++] is evaluated and used before count is incremented. If we wanted the incrementing to occur before evaluation, we would write ++count instead.
Page 25: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

The Other Bag Operations

Read Section 3.1 for the implementations of the other bag member functions.

Remember: If you are just using the bag class, then you don’t need to know how the operations are implemented.

Later we will reimplement the bag using more efficient algorithms.

We’ll also have a few other operations to manipulate bags.

Presenter
Presentation Notes
You can read Section 3.1 for the complete bag implementation. However, the bag class itself is not particularly important; the important point is the concept of a container class, and the advantages that classes provide. Later we will reimplement this same bag in more efficient ways.
Page 26: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

Other Kinds of Bags

In this example, we have implemented a bag containing integers.

But we could have had a bag of float numbers, a bag of characters, a bag of strings . . . Suppose you wanted one of these other bags. How much would you need to change in the implementation ? Section 3.1 gives a simple solution using the C++ typedef statement.

Presenter
Presentation Notes
Here’s one last question for you to think about. Of course, the answer is that there is very little difference between a bag of integers and a bag of any other type. For more on this issue, you should read about typedef statements in Section 3.1. Typedef statements are the first simple way to write a container class where the underlying data type can be easily changed. Later (in Chapter 6) we will see a more powerful alternative to typedef statements.
Page 27: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

A container class is a class that can hold a collection of items.

Container classes can be implemented with a C++ class.

The class is implemented with a header file (containing documentation and the class definition) and an implementation file (containing the implementations of the member functions).

Other details are given in Section 3.1, which you should read.

Summary

Presenter
Presentation Notes
A quick summary . . .
Page 28: Types of Containers: Sequences - Kent State Universitypersonal.kent.edu/.../chapt03_Sequences.pdf · Sequence typedef ____ value_type sequence::value_type is the data type of the

THE END

Presentation copyright 2010, Addison Wesley Longman For use with Data Structures and Other Objects Using C++ by Michael Main and Walter Savitch. Some artwork in the presentation is used with permission from Presentation Task Force (copyright New Vision Technologies Inc.) and Corel Gallery Clipart Catalog (copyright Corel Corporation, 3G Graphics Inc., Archive Arts, Cartesia Software, Image Club Graphics Inc., One Mile Up Inc., TechPool Studios, Totem Graphics Inc.). Students and instructors who use Data Structures and Other Objects Using C++ are welcome to use this presentation however they see fit, so long as this copyright notice remains intact.

Presenter
Presentation Notes