# 14_maps_I - CSCI-1200 Data Structures Fall 2010 Lecture 14...

This preview shows pages 1–3. Sign up to view the full content.

CSCI-1200 Data Structures — Fall 2010 Lecture 14 – Associative Containers (Maps), Part 1 Review from Lecture 13 How to design and implement algorithms using three steps or stages: 1. Generating and Evaluating Ideas 2. Mapping Ideas into Code 3. Getting the Details Right Today’s Class — Associative Containers (Maps) Maps: associative containers for fast insert, access and remove Example: Counting word occurrences Pairs Map iterators Map member functions: operator[] , find , insert , erase . Eﬃciency Maps vs. vectors vs. lists Reading: Ford & Topp, Sections 11.1 & 11.3; 14.1 Maps: Associative Containers Maps store pairs of “associated” values. We will see several examples today, in lab tomorrow, and in Lecture 15: An association between a string, representing a word, and an int representing the number of times that word has been seen in an input ﬁle. An association between a string, representing a word, and a vector that stores the line numbers from a text ﬁle on which that string occurs (next lecture). An association between a phone number and the name of the person with that number (tomorrow’s lab). An association between a class object representing a student name and the student’s info (next lecture). A particular instance of a map is deﬁned (declared) with the syntax: map<key_type, value_type> var_name In our ﬁrst two examples above, key type is a string. In the ﬁrst example, the value type is an int and in the second it is a vector<int> . Entries in maps are pairs : pair<const key_type, value_type> Map iterators refer to pairs. Map search, insert and erase are all very fast: O (log n ) time, where n is the number of pairs stored in the map. Let’s see how this some of this works with a program to count the occurrences of each word in a ﬁle. We’ll look at more details and more examples later.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
14.2 Counting Word Occurrences Here’s a simple and elegant solution to this problem using a map: #include <iostream> #include <map> #include <string> int main() { std::string s;
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/21/2012 for the course CSCI 1200 taught by Professor Cutler during the Fall '08 term at Rensselaer Polytechnic Institute.

### Page1 / 4

14_maps_I - CSCI-1200 Data Structures Fall 2010 Lecture 14...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online