Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Beginning C++ Through Game Programming, Second Edition
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
More on the STL vector list stack queue priority_queue.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
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.
C++ for Engineers and Scientists Third Edition
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11a. The Vector Class.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Templates and the STL.
Object Oriented Data Structures
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Standard Template Library Programming paradigm: generic programming the decomposition of programs into components which may be developed separately and.
Dr. Yingwu Zhu STL Vector and Iterators. STL (Standard Template Library) 6:14:43 AM 2 A library of class and function templates Components: 1. Containers:
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
STL !!!generic programming!!! Anar Manafov
Data Structures Using C++ 2E
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Containers Overview and Class Vector
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Arrays and Containers Mechanism for representing lists.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Pointers and Dynamic Objects Mechanisms for developing flexible list representations JPC and JWD © 2002 McGraw-Hill, Inc.
Generic Programming Using the C++ Standard Template Library.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan and A. Ranade.
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
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,
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
11-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
Lecture 7 : Intro. to STL (Standard Template Library)
A gentle introduction to the standard template library (STL) Some references:
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.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
Object-Oriented Programming (OOP) Lecture No. 41
Motivation for Generic Programming in C++
CSCE 210 Data Structures and Algorithms
C++ Standard Library.
Standard Template Library (STL)
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
priority_queue<T>
Mechanism for representing lists
Lists - I The List ADT.
Lists - I The List ADT.
Iterators and STL Containers
Chapter 3 Lists, Stacks, and Queues
Presentation transcript:

Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Standard Template Library What is it? Collection of container types and algorithms supporting basic data structures What is a container? A generic list representation allowing programmers to specify which types of elements their particular lists hold  Uses the C++ template mechanism Have we seen this library before? String class is part of the STL

Sequences deque, list, and vector  Vector supports efficient random-access to elements STL Container Classes Associative map, set Adapters priority_queue, queue, and stack

Provides list representation comparable in efficiency to arrays Vector Class Properties First-class type Efficient subscripting is possible Indices are in the range 0 … size of list - 1 List size is dynamic Can add items as we need them Index checking is possible Through a member function Iterators Efficient sequential access

Construction Basic construction vector List; Example vector A; // 0 ints vector B; // 0 floats vector C;// 0 Rationals Template classes Classes which can be instantiated with a specified type How to define such classes? See chapter 14 of Cohoon, won’t be covered in this course Base element type Container name

Some Vector Constructors vector () The default constructor creates a vector of zero length vector (size_type n, const T &val = T()) Explicit constructor creates a vector of length n with each element initialized to val E.g. int n = PromptAndRead(); vector D(n); // n ints, uninitialized vector E(n, 3); // n ints initialized to 3 vector R(5); // 5 rationals, default // initialization Rational r(2,3); vector S(5, r); // 5 2/3s

Example #include using namespace std; int main() { vector A(4, 0); // A: A.resize(8, 2); // A: vector B(3, 1); // B: for (int i = 0; i < B.size(); ++i) { A[i] = B[i] + 2; } // A: A = B; // A: return 0; }

Vector Interface size_type size() const Returns the number of elements in the vector cout << A.size(); // display 3 bool empty() const Returns true if there are no elements in the vector; otherwise, it returns false if (A.empty()) { //...

Vector Interface vector & operator = (const vector &V) The member assignment operator makes its vector representation an exact duplicate of vector V.  “Shallow copy” (will see what this means later) The modified vector is returned vector A(4, 0); // A: vector B(3, 1); // B: A = B; // A: vector C(B); vector(const T &V) The copy constructor creates a vector that is a duplicate of vector V.  Shallow copy

Vector.at(i) vector A(4, 0); // A: for (int i = 0; i <= A.size(); ++i) { A[i] = 3; } // A: ?? for (int i = 0; i <= A.size(); ++i) { A.at(i) = 3; } // program terminates // (“exception”) when i is 4 vector.at(i) If i is in bounds, returns a reference to element i of the vector; otherwise, throws an exception vector[i] behaves like array[i], doesn’t check for out of bounds

Vector Interface void resize(size_type s, T val = T()) The number of elements in the vector is now s.  To achieve this size, elements are deleted or added as necessary Deletions if any are performed at the end Additions if any are performed at the end New elements have value val vector A(4, 0); // A: A.resize(8, 2); // A: A.resize(3,1); // A: 0 0 0

Vector Interface pop_back() Removes the last element of the vector push_back(const T &val) Inserts a copy of val after the last element of the vector

Example void GetValues(vector &A) { A.resize(0); int Val; while (cin >> Val) { A.push_back(Val); // A grows as required! } vector List; cout << "Enter numbers: "; GetValues(List);

Iterators Iterator is a pointer to an element Really pointer abstraction Mechanism for sequentially accessing the elements in the list Alternative to subscripting There is an iterator type for each kind of vector list Notes Algorithm component of STL uses iterators Code using iterators rather than subscripting can often be reused by other objects using different container representations

Vector Interface iterator begin() Returns an iterator that points to the first element of the vector iterator end() Returns an iterator that points to immediately beyond the last element of the vector vector C(4); // C: C[0] = 0; C[1] = 1; C[2] = 2; C[3] = 3; vector ::iterator p = C.begin(); vector ::iterator q = C.end();

Iterators To avoid unwieldy syntax programmers often use typedef statements to create simple iterator type names typedef vector ::iterator viiterator; vector C(4); // C: viiterator p = C.begin(); viiterator q = C.end();

Iterator Operators * dereferencing operator Produces a reference to the object to which the iterator p points *p ++ point to next element in list Iterator p now points to the element that followed the previous element to which p points ++p -- point to previous element in list Iterator p now points to the element that preceded the previous element to which p points -- p

viiterator p = C.begin(), q = C.end(); for(;p!=q; p++) { cout << *p << endl; } for(int i=0; i < 10; i++) { cout << C[i] << endl;} int A[10]; for(int * p = A, i =0; i < 10; i++, p++) { cout << *p << endl; }

Maps (not the geographic variety!)

Maps: Generalized Arrays Why should array indices be only integers? How about having an array of names, indexed by roll numbers, even though roll numbers are not integers? In some languages (but not in C++), you can do the following array A[]; A[“ ”] = “Mohan”; A[“ ”] = “Meena”; foreach (a in A) { print a }

Maps in C++ #include using namespace std; int main () { map A; A[“ ”] = “Mohan”; A[“ ”] = “Meena”; cout << A[“ ”]; // show content: rollno => name map ::iterator it; for ( it = A.begin(); it != A.end(); it++ ) cout " << (*it).second << endl; return 0; }

Map usage Many uses E.g. efficiently look up records based on key  (no need to keep sorted array + binary search, etc)  E.g. map // class Entry defined earlier E.g. store property value pairs  Generalization of structs, where field names can be determined at run time  E.g. map A[100]; A[0][“rollno”]=“ ”; A[0][“name”] = “Mohan”; A[1][“rollno”]=“ ”; A[1][“name”] = “Meena”; A[1][“phone”] = “ ”;

Map Implementation Linked list implementation Each node contains key and value Search/insert straightforward but expensive (linear time) Idea: Hash Map implementation 1. create an array of N linked lists 2. given key K, compute a “hash” function which returns a value hash(K) in 0..N-1 3. store key in linked list hash(K) 4. when searching, look in linked list hash(K) Efficiency: If N is chosen to be comparable to total number of elements in linked lists, each list expected to be small

Hash Map head

Homework (Due in 1 week) Give a class definition for HashMap, for the case where the key is a string and the value a string, and only the following methods are to be provided:  insert (string key, string value)  string lookup( string key) Write code for the above methods Define the function hash as follows  Cast each character in the string to an int, and add up the resulting ints. Return the sum modulo N, where N is the size of the array.

Other Features of STL STL: Standard Template Library Other Containers list queue priority_queue bitset

More STL Algorithms Algorithms are mostly on containers/iterators Sorting E.g. vector v; sort(v.begin(), v.end()); or define your own comparison function bool cmp(int a, int b) { return a > b;} sort(v.begin(), v.end(), cmp); Set operations intersection, union, difference search unique: remove duplicates permutations shuffle

Extra Slides

Construction Basic construction vector List(SizeExpression); Example vector A(10); // 10 ints vector B(20); // 20 floats vector C(5);// 5 Rationals int n = PromptAndRead(); vector D(n); // n ints Base element type Container name Number of elements to be default constructed

Construction Basic construction vector List(SizeExpression, Value); Example vector A(10, 3); // 10 3s vector B(20, 0.2); // s Rational r(2/3); vector C(5, r); // 5 2/3s Base element type Container name Number of elements to be default constructed Initial value

Example vector A(4, 0); // A: const vector B(4, 0); // B: for (int i = 0; i < A.size(); ++i) { A[i] = 3; } // A: for (int i = 0; i < A.size(); ++i) { cout << A[i] << endl; // lvalue cout << B[i] << endl; // rvalue }

Function Examples void GetList(vector &A) { int n = 0; while ((n > A[n])) { ++n; } A.resize(n); // Cut off empty part of vector } vector MyList(3); cout << "Enter numbers: "; GetList(MyList);

Examples void PutList(const vector &A) { for (int i = 0; i < A.size(); ++i) { cout << A[i] << endl; } cout << "Your numbers: "; PutList(MyList)

Example: vector of strings Consider vector A; which is set in the following manner A[0]: "A" A[1]: "list" A[2]: "of" A[3]: "words" A[4]: "to" A[5]: "be" A[6]: "read."

Counting o’s The following counts number of o’s within A count = 0; for (int i = 0; i < A.size(); ++i) { for (int j = 0; A[i].size(); ++j) { if (A[i][j] == 'o') { ++count; } To reference j th character of A[i] we need double subscripts Size of A[i] Size of A

Two-Dimensional List E.g. 2 dimensional array: int A[n][n] For 2 dimensional vectors, consider definition vector > A; Then A is a vector > It is a vector of vectors A[i] is a vector i can vary from 0 to A.size() - 1 A[i][j] is a int j can vary from 0 to A[i].size() - 1

More Vector Ops vector ::reverse_iterator typedef vector ::reverse_iterator reverse_iterator; typedef vector ::const_reference const_reference;

typedef vector ::iterator iterator; typedef vector ::reverse_iterator reverse_iterator; vector List(3); List[0] = 100; List[1] = 101; List[2] = 102; iterator p = List.begin(); cout << *p; // p; cout << *p; // p; cout << *p; // 100 reverse_iterator q = List.rbegin(); cout << *q; // q; cout << *q; // q; cout << *q; // 102