Associative Containers and Iterators. Ali Malik

Similar documents
Advanced Associative Containers. Ali Malik

Sequential Containers. Ali Malik

Sequential Containers. Ali Malik

Associative Containers & Iterators. Cristian Cibils

Template based set of collection classes STL collection types (container types)

SSE2034: System Software Experiment 3

CSCI 104 C++ STL; Iterators, Maps, Sets. Mark Redekopp David Kempe

CS197c: Programming in C++

auto map<deque<string>, vector<string>> mymap; for(auto iter = mymap.begin(); iter!= mymap.end(); ++iter) {

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

STL Quick Reference for CS 241

EE 355 Unit 10. C++ STL - Vectors and Deques. Mark Redekopp

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

Functions. Ali Malik

STL Containers, Part II

Container Notes. Di erent Kinds of Containers. Types Defined by Containers. C++11 Container Notes C++11

Sequence Containers. Cristian Cibils

C++ Standard Template Library. Contents. Pierre Fierz. 1 Introduction. 2 Organization of STL Header Files. Organization of STL Header Files

TDDD38 - Advanced programming in C++

CS 103 Unit 12 Slides

Standard Template Library

CSCI 104 C++ STL; Iterators, Maps, Sets. Mark Redekopp David Kempe

Functions. Ali Malik

Dynamic Data Structures

Lectures 11,12. Online documentation & links

CS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

Chapter 5. The Standard Template Library.

6.096 Introduction to C++

CS 103 Unit 11. Linked Lists. Mark Redekopp

STL: C++ Standard Library

Ali Malik Handout 12 CS106L Winter 2018 Jan 30th, C++ Reference

EE 355 Unit 11b. Doubly-Linked Lists and Deques. Mark Redekopp

CSE 100: C++ TEMPLATES AND ITERATORS

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Lectures 19, 20, 21. two valid iterators in [first, last) such that i precedes j, then *j is not less than *i.

Modern and Lucid C++ for Professional Programmers. Part 7 Standard Containers & Iterators. Department I - C Plus Plus

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2

CS11 Advanced C++ Fall Lecture 1

Exceptions, Templates, and the STL

Lecture 4a Using The Standard Template Library. Jack Applin, Guest Lecturer

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Standard Template Library

CS 103 Unit 11. Linked Lists. Mark Redekopp

The Standard Template Library Classes

CSE 100: C++ TEMPLATES AND ITERATORS

Sets and MultiSets. Contents. Steven J. Zeil. July 19, Overview of Sets and Maps 4

Chapter 17: Linked Lists

EL2310 Scientific Programming

Prefix Trees Tables in Practice

Lesson 13 - Vectors Dynamic Data Storage

CPSC 427a: Object-Oriented Programming

Const Correctness. Ali Malik

STL Containers, Part I

PIC 10A. Lecture 23: Intro to STL containers

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists

Polymorphism. Programming in C++ A problem of reuse. Swapping arguments. Session 4 - Genericity, Containers. Code that works for many types.

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists

CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation

Sixth lecture; classes, objects, reference operator.

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

Containers in C++ and Java

1. The term STL stands for?

The Standard Template Library. EECS 211 Winter 2018

EL2310 Scientific Programming

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

! Search: find a given target item in an array, ! Linear Search: Very simple search method: ! Operators such as =, +, <, and others can be

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

Programming Abstractions

2014 Spring CS140 Final Exam - James Plank

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

CS183 Software Design. Textbooks. Grading Criteria. CS183-Su02-Lecture 1 20 May by Eric A. Durant, Ph.D. 1

To know the relationships among containers, iterators, and algorithms ( 22.2).

EE 355 Unit 13 C++ STL; Iterators, Maps, Sets. Mark Redekopp

C++ Programming Lecture 2 Software Engineering Group

CSE100. Advanced Data Structures. Lecture 4. (Based on Paul Kube course materials)

Programming Abstractions

Using Associative Containers

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

Outline. Variables Automatic type inference. Generic programming. Generic programming. Templates Template compilation

CSC 427: Data Structures and Algorithm Analysis. Fall 2004

Programming Abstractions

STL. Prof Tejada Week 14

Queue Implementations

C++: Overview and Features

CSE 250: Data Structures Fall 2015 Sample Final Exam

Lecture on pointers, references, and arrays and vectors

Chapter 17: Linked Lists

Patterns: Working with Arrays

Pointers, Dynamic Data, and Reference Types

Exam 3 Chapters 7 & 9

Exercise 6.2 A generic container class

COMP6771 Advanced C++ Programming

Lecture 12. Monday, February 7 CS 215 Fundamentals of Programming II - Lecture 12 1

EL2310 Scientific Programming

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Templates and Vectors

CSE 100: C++ TEMPLATES AND ITERATORS

CSI33 Data Structures

Transcription:

Associative Containers and Iterators Ali Malik malikali@stanford.edu

Game Plan Recap Associative Containers Iterators Map Iterators The auto keyword (maybe) Range-Based for Loops (maybe)

Recap

Structs You can define your own mini-types that bundle multiple variables together: struct point { int x; int y; }; Useful for Assignment 1

Overview of STL Algorithms Iterators Allocators Containers Functors/Lambdas Adapters

Overview of STL Algorithms Iterators Allocators Containers Functors/Lambdas Adapters

Sequence Containers Provides access to sequences of elements. Examples: std::vector<t> std::list<t> std::deque<t>

std::vector<t> Some new stuff there: const int knumints = 5; using vecsz_t = std::vector<int>::size_type; std::sort(vec.begin(), vec.end()); This let s us use vecsz_t as an alias/synonym for the type std::vector<int>::size_type;

std::deque<t> A deque (pronounced deck ) is a double ended queue. Can do everything a vector can do and also Unlike a vector, it is possible (and fast) to push_front and pop_front.

Container Adapters How can we implement stack and queue using the containers we have? Stack: Just limit the functionality of a vector/deque to only allow push_back and pop_back. Queue: Just limit the functionality of a deque to only allow push_back and pop_front. Plus only allow access to top element

Associative Containers

Associative Container Scenario: You want to count the frequency of words in a file. What do you use? vector<string>, vector<int>

Associative Container Scenario: You want to count the frequency of words in a file. What do you use? vector<string>, vector<int> We would need to keep two vectors with indexes denoting pairs

Associative Container Scenario: You want to count the frequency of words in a file. What do you use? vector<string>, vector<int>

Associative Container Scenario: You want to count the frequency of words in a file. What do you use? vector<string>, vector<int> vector<std::pair<string,int>>

Associative Container Scenario: You want to count the frequency of words in a file. What do you use? vector<string>, vector<int> vector<std::pair<string,int>> No quick way to lookup based on word

Associative Container Scenario: You want to count the frequency of words in a file. What do you use? vector<string>, vector<int> vector<std::pair<string,int>>

Associative Container Scenario: You want to count the frequency of words in a file. What do you use? vector<string>, vector<int> vector<std::pair<string,int>> std::map<string, int>

Associative Container Scenario: You want to count the frequency of words in a file. What do you use? vector<string>, vector<int> vector<std::pair<string,int>> std::map<string, int>

Associative Container It is useful to have a data structure that can associate values. Associative containers do exactly that!

Associative Container Have no idea of a sequence. Data is accessed using the key instead of indexes. std::map<t1, T2> std::set<t> std::unordered_map<t1, T2> std::unordered_set<t>

Associative Container Have no idea of a sequence. Data is accessed using the key instead of indexes. std::map<t1, T2> std::set<t> std::unordered_map<t1, T2> std::unordered_set<t>

Associative Container Have no idea of a sequence. Data is accessed using the key instead of indexes. std::map<t1, T2> std::set<t> std::unordered_map<t1, T2> std::unordered_set<t> Based on ordering property of keys. Keys need to be comparable using < (less than) operator.

Associative Container Have no idea of a sequence. Data is accessed using the key instead of indexes. std::map<t1, T2> std::set<t> std::unordered_map<t1, T2> std::unordered_set<t>

Associative Container Have no idea of a sequence. Data is accessed using the key instead of indexes. std::map<t1, T2> std::set<t> std::unordered_map<t1, T2> std::unordered_set<t>

Associative Container Have no idea of a sequence. Data is accessed using the key instead of indexes. std::map<t1, T2> std::set<t> std::unordered_map<t1, T2> std::unordered_set<t> Based on hash function. You need to define how the key can be hashed.

Associative Container Have no idea of a sequence. Data is accessed using the key instead of indexes. std::map<t1, T2> std::set<t> std::unordered_map<t1, T2> std::unordered_set<t>

Associative Container Have no idea of a sequence. Data is accessed using the key instead of indexes. std::map<t1, T2> std::set<t> std::unordered_map<t1, T2> std::unordered_set<t> You can define < and hash function operators for your own classes!

std::map<t1, T2> Methods mostly same as Stanford map. Check documentation for full list of methods. We can do a small example, counting frequency of words in a file: Map Example (MapExample.pro)

std::set<t> Methods mostly same as Stanford map. Check documentation for full list of methods. Key point: A set is just a specific case of a map that doesn t have a value. We can do a small example of adding and removing stuff: Set Example (SetExample.pro)

Announcements

Game Plan Recap Associative Containers Iterators Map Iterator The auto keyword Range-Based for Loops

Iterators How do we iterate over associative containers? Remember: Assoc. containers have no notion of a sequence/indexing for(int i = umm?; i < uhh?; i++ maybe?) {

Iterators How do we iterate over associative containers? Remember: Assoc. containers have no notion of a sequence/indexing for(int i = umm?; i < uhh?; i++ maybe?) {

Iterators How do we iterate over associative containers? Remember: Assoc. containers have no notion of a sequence/indexing for(int i = umm?; i < uhh?; i++ maybe?) { C++ has a solution!

Iterators

First: A note

A note We are going on a journey.

A note

A note We could in theory fly to the end destination.

A note

A note But then we would miss the experience

A note We are going on a journey.

A note We are going on a journey. At the end lies simplicity, but if we jump to it we miss out on understanding.

A note We are going on a journey. At the end lies simplicity, but if we jump to it we miss out on understanding. So we will walk to it.

Iterators

Iterators Iterators allow iteration over any container, whether it is ordered or not.

Iterators Let s try and get a mental model of iterators: Say we have a std::set<int> myset

Iterators Let s try and get a mental model of iterators: Say we have a std::set<int> myset 2 1 3

Iterators Let s try and get a mental model of iterators: Say we have a std::set<int> myset 2 1 3 1 2 3

Iterators Let s try and get a mental model of iterators: Say we have a std::set<int> myset Iterators let us view a non-linear collection in a linear manner. 2 1 3 1 2 3

Iterators How do they work? We don t care right now. We will just use them like any other thing - assume they just work somehow.

Iterators - Usage Let s try and get a mental model of iterators: 12 1 2 3

Iterators - Usage Let s try and get a mental model of iterators: We can get an iterator pointing to the start of the sequence by calling myset.begin() 12 1 2 3 myset.begin();

Iterators - Usage Let s try and get a mental model of iterators: 12 1 2 3 myset.begin();

Iterators - Usage Let s try and get a mental model of iterators: 12 1 2 3 How do we store it in a variable? myset.begin();

Iterators - Usage Let s try and get a mental model of iterators: 12 1 2 3??? iter = myset.begin();

Iterators - Usage Let s try and get a mental model of iterators: 1 12 2 3 What is the type of the iterator???? iter = myset.begin();

Iterators - Usage Let s try and get a mental model of iterators: 1 12 2 3 What is the type of the iterator???? iter = myset.begin();

Iterators - Usage Let s try and get a mental model of iterators: 12 1 2 3 3??? iter = myset.begin();

Iterators - Usage Let s try and get a mental model of iterators: 1 12 2 3 set<int>::iterator iter = myset.begin();

Iterators - Usage Let s try and get a mental model of iterators: It is the iterator type defined in the set<int> class! 1 12 2 3 set<int>::iterator iter = myset.begin();

Iterators - Usage Let s try and get a mental model of iterators: 1 12 2 3 set<int>::iterator iter = myset.begin();

Iterators - Usage Let s try and get a mental model of iterators: 12 1 iter 2 3

Iterators - Usage Let s try and get a mental model of iterators: We can get the value of an iterator by using the dereference * operator. 12 1 iter 2 3

Iterators - Usage Let s try and get a mental model of iterators: We can get the value of an iterator by using the dereference * operator. 12 1 2 3 iter cout << *iter << endl; // prints 1

Iterators - Usage Let s try and get a mental model of iterators: 12 1 iter 2 3

Iterators - Usage Let s try and get a mental model of iterators: We can advance the iterator one by using the ++ operator (prefix) 12 1 iter 2 3

Iterators - Usage Let s try and get a mental model of iterators: We can advance the iterator one by using the ++ operator (prefix) 12 1 2 3 iter ++iter; // advances iterator

Iterators - Usage Let s try and get a mental model of iterators: And so on... 12 1 2 iter 3

Iterators - Usage Let s try and get a mental model of iterators: And so on... 12 1 2 3 iter cout << *iter << endl; // prints 2

Iterators - Usage Let s try and get a mental model of iterators: And so on... 12 1 2 iter 3

Iterators - Usage Let s try and get a mental model of iterators: And so on... 12 1 2 3 iter ++iter; // advances iterator

Iterators - Usage Let s try and get a mental model of iterators: And so on... 12 1 2 3 iter

Iterators - Usage Let s try and get a mental model of iterators: And so on... 12 1 2 3 iter cout << *iter << endl; // prints 3

Iterators - Usage Let s try and get a mental model of iterators: And so on... 12 1 2 3 iter

Iterators - Usage Let s try and get a mental model of iterators: And so on... 12 1 2 3 iter ++iter; // advances iterator

Iterators - Usage Let s try and get a mental model of iterators: And so on... 12 1 2 3 iter

Iterators - Usage Let s try and get a mental model of iterators: And so on... 12 1 2 3 iter cout << *iter << endl; // prints

Iterators - Usage Let s try and get a mental model of iterators: And so on... 1 12 2 3 iter

Iterators - Usage Let s try and get a mental model of iterators: And so on... 1 12 2 3 iter ++iter; // advances iterator

Iterators - Usage Let s try and get a mental model of iterators: 1 12 2 3 iter

Iterators - Usage Let s try and get a mental model of iterators: We can check if we have hit the end by comparing to myset.end() 1 12 2 3 iter

Iterators - Usage Let s try and get a mental model of iterators: We can check if we have hit the end by comparing to myset.end() 1 12 2 3 iter if(iter == myset.end()) return;

Iterators - Usage A summary of the essential iterator operations: Create iterator Dereference iterator to read value currently pointed to Advance iterator Compare against another iterator (especially.end() iterator)

Iterators - Usage Let s do some examples: Basic Iterator (BasicIter.pro)

Iterators Our examples have used sets, but (almost) all C++ containers have iterators. Why is this powerful? Many scenarios require looking at elements, regardless of what type of container is storing those elements. Iterators let us go through sequences of elements in a standardised way.

Iterators Example (find number occurrences): int numoccurences(vector<int>& cont, int elemtocount) { int counter = 0; vector<int>::iterator iter; for(iter = cont.begin(); iter!= cont.end(); ++iter) { if(*iter == elemtocount) ++counter; } return counter; }

Iterators Example (find number occurrences): Can I make this work for std::list<int>? int numoccurences(vector<int>& cont, int elemtocount) { int counter = 0; vector<int>::iterator iter; for(iter = cont.begin(); iter!= cont.end(); ++iter) { if(*iter == elemtocount) ++counter; } return counter; }

Iterators Example (find number occurrences): Can I make this work for std::list<int>? int numoccurences(vector<int>& cont, int elemtocount) { int counter = 0; vector<int>::iterator iter; for(iter = cont.begin(); iter!= cont.end(); ++iter) { if(*iter == elemtocount) ++counter; } return counter; }

Iterators Example (find number occurrences): int numoccurences(list<int>& cont, int elemtocount) { int counter = 0; list<int>::iterator iter; for(iter = cont.begin(); iter!= cont.end(); ++iter) { if(*iter == elemtocount) ++counter; } return counter; }

Iterators This standard interface for looping through things is going to be really powerful. We will cover it sometime this week or next week!

Map Iterators

Map Iterators Map iterators are slightly different because we have both keys and values. The iterator of a map<string, int> points to a std::pair<string, int>.

The std::pair Class A pair is simply two objects bundled together. Syntax: std::pair<string, int> p; p.first = "Phone number"; p.second = 6505500;

Map Iterators Example: map<int, int> m; map<int, int>::iterator i = m.begin(); map<int, int>::iterator end = m.end(); while(i!= end) { cout << (*i).first << (*i).second << endl; ++i; }

Next Time Templates and Iterators