CHAPTER 11 CS 3370 – C++ Associative Containers. The ordered containers used tree-based storage O(log n) retrieval complexity The unordered containers.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

Chapter 2: Basic Elements of C++
1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE Lecture 15 – Maps.
Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative.
1 Associative Containers Gordon College Prof. Brinton.
1 Associative Containers Gordon College Prof. Brinton.
Chapter 4 Summation.
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
Review of C++ Programming Part II Sheng-Fang Huang.
CSE 332: C++ Associative Containers II Associative Containers’ Associated Types Associative containers declare additional types –A key_type gives the type.
Standard Template Library Programming paradigm: generic programming the decomposition of programs into components which may be developed separately and.
Binary Search Trees II Morse Code.
Containers and Iterators CNS 3370 Copyright 2003, Fresh Sources, Inc.
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
The C++ Standard Template Library. What is STL A subset of the standard C++ library –The string class is not part of it! 3 major components –Algorithms.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray,
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming.
11-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
1 Joe Meehean.  List of names  Set of names  Map names as keys phone #’s as values Phil Bill Will Phil Bill Will Phil Bill Will Phil: Bill:
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
STL – Standard Template Library L. Grewe. 2 Goals Lots of important algorithms, data structures in CS using Templates. is a software library partially.
1 Associative Containers Ordered Ordered Unordered UnorderedSets Maps as sets of pairs Set API Ex: Sieve of Eratosthenes Ex: Sieve of EratosthenesImplementation.
The Standard Template Library Container Classes Version 1.0.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
STL Associative Containers navigating by key. Pair Class aggregates values of two, possibly different, types used in associative containers defined in.
CS 240Chapter 10 – TreesPage Chapter 10 Trees The tree abstract data type provides a hierarchical to the representation of certain types of relationships.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Main Index Contents 11 Main Index Contents Sets Defined by a key along with other data Sets Defined by a key along with other data Key-Value Data Key-Value.
CPSC 252 Tables / Maps / Dictionaries Page 1 Tables, Maps and Dictionaries A table (or map or dictionary) is a collection of key/value pairs. In general.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Object-Oriented Programming (OOP) Lecture No. 41
Introduction to olympic programming
CSCE 210 Data Structures and Algorithms
Topic Pre-processor cout To output a message.
Programming Abstractions
Standard Template Library (STL)
Hash Tables.
What remains Topics Assignments Final exam
Chapter 9 – Sets and Maps 9.1 Associative Container
L09 - Pokémon.
Elements are always copied when they are put into a container
STL Containers Some other containers in the STL.
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
Engineering Problem Solving with C++, Etter
Pointers & Dynamic Data Structures
Iterators and STL Containers
Chapter 2: Introduction to C++.
Arithmetic Operations
STL Библиотека стандартных шаблонов
Standard Template Library
Standard Template Library
An Introduction to STL.
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Recitation Outline Hash tables in C++ STL Examples Recursive example
Chapter 9 – Sets and Maps 9.1 Associative Container
9.2 Maps and Multimaps 9.3 Hash Tables 9.2, pgs
Standard Template Library
Data Structures & Programming
Presentation transcript:

CHAPTER 11 CS 3370 – C++ Associative Containers

The ordered containers used tree-based storage O(log n) retrieval complexity The unordered containers are hash tables O(1) retrieval complexity

Ordered Set Example #include using namespace std; int main() { // Populate a set: set s; s.insert("Alabama"); s.insert("Georgia"); s.insert("Tennessee");

// Print it out: auto p = s.begin(); while (p != s.end()) cout << *p++ << endl; cout << endl; // Do some searches: string key = "Alabama"; p = s.find(key); cout << (p != s.end() ? "found " : "didn't find ") << key << endl; key = "Michigan"; p = s.find(key); cout << (p != s.end() ? "found " : "didn't find ") << key << endl; }

// Output: Alabama Georgia Tennessee found Alabama didn't find Michigan

Word Count Program

std::pair Operations Defined in

Another Map Example #include using namespace std; int main() { // Insert some elements (three ways): map > m; m.insert(make_pair("Alabama","Montgomery")); m.insert({"Tennessee","Knoxville"}); m["Georgia"] = "Atlanta"; m["Tennessee"] = "Nashville"; // Overwrites

// Print the map: for (const auto& elem: m) cout << '{' << elem.first << ',' << elem.second << "}\n"; cout << endl;

// Retrieve via a key: cout << '"' << m["Georgia"] << '"' << endl; cout << m.size() << endl; cout << '"' << m["Texas"] << '"' << endl; cout << m.size() << endl; } // Output: {Tennessee,Nashville} {Georgia,Atlanta} {Alabama,Montgomery} "Atlanta" 3 "" 4

Insert Operations

Erase Operations

Strict Partial Orderings A Model of “Less-than” priority_queue and ordered associative containers (set, map, multi_set, multi_map) require strict partial ordering comparators AKA “strict weak order” ( behaves like less ( ) ( (which calls operator<( )) never use <= or anything like it!!! Definition: f(x,y) is a SPO if: f(x,x) = false(irreflexive) f(x,y) = !f(y,x)(anti-symmetric) f(x,y) && f(y,z) => f(x,z)(transitive)

map and set use SPOs! Necessary to maintain proper order in the underlying tree data structure They test for equivalence, not equality, to maintain uniqueness x and y are equivalent iff !cmp(x,y) && !cmp(y,x) i.e., neither precedes the other Example: swo.cpp ignores non-alpha characters in strings

Another Example Creates a last/first key type Defines a strict-partial bool operator( ) See records.cpp Note: ALL containers require a default constructor of their containees! Use =default if applicable

Unordered Container Operations