Don't get to bogged down in the early sections. Do a fast scan of the whole chapter and make a list of all the different types of containers that the C++ Standard Library provides. Then drill down for the details.
You can skip examples that use the author's alpha class. These are set up to work in Europe as well as the USA.
Don't panic: We will look at algorithms in general next time [ 13.html ] , and then return and study C++ algorithms and iterators [ 14.html ] after that. Then we will study how the <list> data structure is implemented.
Then there will be a quiz that tests your knowledge of the standard containers: deque, list, map, multimap, queue, set, multiset, stack, vector,... I will describe a situation or property and expect you to select a suitable container. For example
| Name | Library | Notes |
|---|---|---|
| vector | <vector> | indexed contiguous sequence |
| deque | <deque> | vector grows at both ends |
| stack | <stack> | LIFO, push, pop, top, empty, size |
| queue | <queue> | FIFO, push, pop, first, last, size, empty |
| priority_queue | <queue> | Biggest floats to top, push, pop, first, last, size, empty |
| list | <list> | slice, dice, insert, sort, ... |
| set | <set> | ordered collection of unique items, intersections, union, etc |
| multiset | <set> | ordered collection of items, intersections, union, etc |
| map | <map> | ordered set of pairs, first key to unique items,..., general array |
| multimap | <map> | ordered set of pairs, first key to items,..., general array |
| functions | various | Yes, functions are also objects. There are functions that have functions as data and produce functions! |
| streams | <iterator> | Access files, cin, cout using iterators and algorithms |
In your future career you may need any of the containers or algorithms:-(
In our degrees knowing these containers and iterators can make many classes
easier.
Are containers, iterators, and algorithms used in internet applications?
Yes. Internet applciations should have a specialized fron end that
interacts with the user and sends them pages to view. Each
internet technology has
different ways of handling this.
Inside each application are search engines, data bases, containers,
iterators, and algorithms -- and nearly all of them come from
computer science.
What does an fstream do?
It allows input and out of data from and to a file.
Can I explain how iterators work?
Later. We will design and implement some in CS202.
Is it beneficial to write algorithms out instead of using the library?
Onely in a class like CS330!
What are the associative containers?
Set, map, multiset, multimap. See questions below.
What is a set?
A collection of objects. You can insert and remove them. You can test
if an object (or another set) is in a set. C++ sets are kept in order
and you can visit each member in turn. However you can not have duplicate
elements.
Example: The set of students on my roster.
What is a multiset?
It is just like a set accept an item can appear zero or more times.
What is a map?
A mathematical map is a relation between two sets of objects called the
domain and codomain. It is a map because each element in the domain has no
more than one element in the codomain. It is well known that any
relationship can be seen as a set of pairs. So, in C++, a map is a set of
pairs like this
pair(x,y)and each x has no more than one y. WHat is cool is that if m is a map associating each X with a Y, then
m [x]is the y. More you can use assignment to add and modify the pairs:
m[x]=y;
set<Phone> directory;then s is set, styp is "set<Phone>", the etyp is "Phone", and the comparator type (ctyp) is "less<Phone>". So "directory" has type "set<Phone>" and directory[3], if it exists, has type "Phone", and we compare "Phone" items like this
phone1.less<Phone>(phone2)
function_object(arguments)In other words
T operator () (arguments){....}
Here is a dumb example
[ testFillGen.cpp ]
of using function objects.
What are function adapters?
A function adapter is a special kind of function that takes another function
and returns a new adapted function that uses the original one.
THink of a the pwer adapters you use when you visit a foreign country.
We won't use them any quizzes or the final but I may be forced to
use one in a lab. You may find them helpful after you graduate.
Why are random access iterators most powerful?
A random access iterator can do everything that a bidirectional
iterator can. And a bidirectional iterator can do everything that
a normal iterator can do:
| Type of Iterator | Operations |
|---|---|
| Random access | *, =, ==, ++, --, +=, -=, [], ... |
| bidirectional | *, =, ==, ++, -- |
| Random access | *, =, ==, ++ |
Often, you can find a predefined container/algorithm that does what you
want better than you could. It is likely to be faster and cleverer
than anything you could do quickly.
What are function adapters?
Function adapters allow us to construct function objects with complicated
behavior without declaring a class or function. They are operators
that manipulate functions rather than numbers or characters.
You shouldn't need to use them in CSci202, but you may find a use for them in your future career. For example, do rapidly develop a version of the quicksort algorithm I needed to use the partition algorithm and that needed me to define a predicate that was true for all items less than or equal to a particular value. To do that I used a function adapter to convert the predefined less_equal function object into the predicate I wanted:
bind2nd(less_equal<int>(), value)Key point: I didn't expect to ever use this clever feature of C++, but did so this week.
Key points?
map<string, int> directory;
directory["Dick"] = 5327;
| Math | C++ example |
|---|---|
| Subset | includes(A1,A2,B1,B2) |
| Union | set_union(A1,A2,B1,B2, C); |
| Intersection | set_intersection(A1,A2,B1,B2, C); |
| Difference | set_difference(A1,A2,B1,B2, C); |
namespace::namebut this gets old quickly. Instead we can write
using namespace whatever;
Some older compilers let you be lazy and leave out the using namespace.
How does a priority queue work?
Magic! Take CSCI330 to get the answers!
Do stream iterators really handle input and output?
Yes. Here is some code I found in a sample program
copy(v.begin(), v.end(), ostream_iterator<int>(cout, "\t"));It outputs every int in the container v with a Tab between them.
See
[ ostream_iterator.cpp ]
for a complete working example of both io-iterators.
Compare four kinds of iterator?
| iterator | The normal way to move across container and change things in it. |
| const_iterator | The normal way to move across a container. Can not change items! |
| reverse_iterator | The other way to move across container and change things in it. |
| const_reverse_iterator | The other way to move across a container. Can not change items! |
. . . . . . . . . ( end of section CSci202 Computer Science II, Session 12 Containers) <<Contents | End>>
Laboratory 6 Information Security
[ lab06.html ]
Next -- Introduction to Algorithms
[ 13.html ]
and read the special handouts
[ alg.html ]
and
[ algorithms.html ]
on algorithms. We will experiment with various data structures
and algorithms in Lab 7.
Special Preparation for Next Class
Bring a deck of cards.
Quiz Next time
Subjects: Exceptions, Arguments to the main function,
formatted input/output to files.