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

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

View Full Document Right Arrow Icon
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 . Efficiency 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 file. An association between a string, representing a word, and a vector that stores the line numbers from a text file 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 defined (declared) with the syntax: map<key_type, value_type> var_name In our first two examples above, key type is a string. In the first 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 file. We’ll look at more details and more examples later.
Background image of page 1

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

View Full DocumentRight Arrow Icon
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;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

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

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online