CS 14 - Lab


CS 14 Homepage

Advanced STL - Associative Containers and Algorithms

In this lab, you will be learning about a few advanced STL topics including associative containers and algorithms. You will do some short exercises to help introduce you to these topics. For this lab you will be working independently. You may want to look at the STL reference guide for more information.

Associative Containers - Maps

The sequential containers you have used thus far (list, vector, and deque) are very useful but they provide you with code that you could easily reproduce in a few hours. However, associative containers provide implementations for data types that you could not so easily duplicate in a few hours of coding. Associative containers (aka associative arrays) store items based on keys. In a compiler, a symbol table can be used to store memory addresses of variables and functions within the code. If you were to write a compiler and you used an associative array for your symbol table, you can easily accesses the memory addresses using syntax such as this:

This is a very intuitive way to code, but C++ does not have built in support for associative arrays (some languages such as Perl have built in support for associative arrays). Luckily, with STL, associative arrays are very simple to use and you can use anything that you want as the index: built-in types, user-defined structures, classes, etc. Associative containers are implemented as red-black trees for efficiency.

Today you will be learning about the map associative container. The map container comes in the form of map<Key, T, Compare> where Key is the key you will use to do lookups, T is the data to store at that location, and Compare is a function object that compares two key values. For maps using built-in types for keys, you can use the less< > function object template:
If the key values are user defined types, you will need to overload operator <().

Part 1 - Learning Maps

For part 1 of your assignment, you will be doing a very simple program involving maps and the number of days in each month. Key will be the month name and T will be the number of days in that month (see above for the syntax used to declare a map). Use the following procedure to complete part 1:

Algorithms

STL also provides you with various templatized algorithms that are very simple to use. Algorithms are simply templatized functions. There are many types of algorithms but I will only cover 2: sort and accumulate.

sort - The sorting algorithm allows you to sort any type of data structure whether it is a built in type of a user defined type. Sort works by using the < operator for the objects being sorted. If you are sorting built-in types the < operator is already defined for you. If you are sorting user defined types, you will need to overload the < for the type. Sort can be used in two different ways:
accumulate - Accumulate repeatedly applies a function to each member of the input sequence and returns the result. For instance, if you want to add up all of the values in an integer array, you can use accumulate to do this. However, accumulate is flexible so that you can specify what function you want to apply to each member of the input (for instance you could muliply all of them together). Accumulating is such a common operation, that many computer architectures have accumulation registers (you will learn more about this in cs61 and cs161). The STL accumulate comes in 2 different offerings:

Part 2 - Learning algorithms

For part 2 of your lab, you will use the sort and the accumulate algorithms. You will create a list of integers and an array of integers and apply these algorithms to both. Use the following procedure to complete part2:

Point Breakdown For Lab Assignment

You will be working alone for this lab. The lab will be due 1 hour after the end of your lab section NEXT WEEK. I am giving you an extra week so that you can get help from the TA if you need it. You may demo your program in lab NEXT WEEK or it will be graded from your electronic submission. Please show the output from part1 and part2 to the TA. The TA will also look at your code for correctness when checking out. Remember, to receive credit for this lab, you MUST turn the code in online. If you do not turn in your code online, you will lose points for your lab. All code must be turned in online for archival purposes.

Remeber to compile your code using the flags "-g -Wall -W -Werror -pedantic". For this lab you do not have to use a makefile since you are creating 2 standalone programs each in a single file.

© 2003 UC Riverside Department of Computer Science & Engineering. All rights reserved.