Types of Containers: Sequences - Kent State...
Transcript of Types of Containers: Sequences - Kent State...
Types of Containers: Sequences
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
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.
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.
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. */
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]
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)
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
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, )
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
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?
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.*/
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.
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();
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();
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.
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
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.
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
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
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)
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
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++;
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;
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.
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.
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
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.