Lecture 14 - Associative Containers

Lecture 14 - Associative Containers - Using Associative...

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

View Full Document Right Arrow Icon
Using Associative Containers Lecture 18 Hartmut Kaiser [email protected] http://www.cct.lsu.edu/˜hkaiser/fall_2011/csc1254.html
Background image of page 1

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

View Full Document Right Arrow Icon
Programming Principle of the Day Single Responsibility Principle - A component of code (e.g. class or function) should perform a single well defined task. Components should have exactly one reason to change . As an example, consider a module that compiles and prints a report. Can be changed for two reasons: The content of the report can change. The format of the report can change. These two things change for very different causes; one substantive, and one cosmetic. The single responsibility principle says that these two aspects of the problem are really two separate responsibilities, and should therefore be in separate components. It would be a bad design to couple two things that change for different reasons at different times. http://en.wikipedia.org/wiki/Single_responsibility_principle 10/27/2011 Lecture 18 CSC 1254, Fall 2011, Using Associative Containers 2
Background image of page 2
Abstract Associative containers arrange their elements in a certain sequence based on an ordering criteria for the elements themselves They employ ordering to quicker find elements Often they store key/value pairs, ordering the values based on the key We will investigate these containers and use maps to write compact and efficient look-up- intensive programs. 10/27/2011 Lecture 18 CSC 1254, Fall 2011, Using Associative Containers 3
Background image of page 3

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

View Full Document Right Arrow Icon
Why Associative Containers? Finding an element in sequential containers entails sequential search ▫ Potentially slow if container has many elements Alternative is to keep elements in sequential container in certain order ▫ Devise a special search strategy, not easy ▫ Potentially slow to insert as it might reorder elements Another alternative is to use associative containers 10/27/2011 Lecture 18 CSC 1254, Fall 2011, Using Associative Containers 4
Background image of page 4
Associative Containers Associative containers automatically arrange their elements into a sequence that depends on the values of the elements themselves, rather than the sequence in which they were inserted Allows to locate element with particular value quickly The part which is used to locate an element is the key , which sometimes has an associated value 10/27/2011 Lecture 18 CSC 1254, Fall 2011, Using Associative Containers 5
Background image of page 5

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

View Full Document Right Arrow Icon
Counting Words Almost trivial with associative containers: int main() { string s; map<string, int > counters; // store each word and an // associated counter // read the input, keeping track of each word and // how often we see it while (cin >> s) ++counters[s]; // write the words and associated counts for (map<string, int >::const_iterator it = counters.begin(); it != counters.end(); ++it) { cout << it->first << "\t" << it->second << endl; } return 0; } 10/27/2011 Lecture 18 CSC 1254, Fall 2011, Using Associative Containers 6
Background image of page 6
Counting Words As std::map holds key/value pairs, we need to
Background image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 27

Lecture 14 - Associative Containers - Using Associative...

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

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