SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab.

Slides:



Advertisements
Similar presentations
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Advertisements

Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
The Standard Template Library – part 2. auto_ptr Regular pointers may cause memory leaks Regular pointers may cause memory leaks void f() { SomeClass.
Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
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.
Quick Overview of STL STL = Standard Template Library Main concept : Container, Iterator Application : Linked list, Stack etc.
Standard Template Library (STL) Overview – Part 1 Yngvi Bjornsson.
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.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
Templates and the STL.
Writing Your Own STL Container Ray Lischner
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of Engineering Cairo University Advanced Programming Spring 2010.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Standard Template Library There are 3 key components in the STL –Containers –Iterators –Algorithms Promote reuse More debugged May be more efficient.
STL !!!generic programming!!! Anar Manafov
Data Structures Using C++ 2E
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Containers Overview and Class Vector
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.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
STL-Associative Containers. Associative Containers Sequence containers : "This is item 0, this is item 1, this is item 2…“ Associative containers : –
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Overview 18.1 Iterators 18.2 Containers 18.3 Generic Algorithms Slide
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
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)
C++ STL CSCI 3110.
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:
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming.
The Utility Class pair All associative containers make use of the pair template, which is defined in. Slightly stripped down, it looks basically like this:
Lecture 7 : Intro. to STL (Standard Template Library)
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
Data Structures for Midterm 2. C++ Data Structure Runtimes.
A gentle introduction to the standard template library (STL) Some references:
1 STL Containers Copyright Kip Irvine, All rights reserved. Only students enrolled in a class at Florida International University may copy or print.
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.
C++ Review STL CONTAINERS.
Exam Review 2 Chapter 5 – 9 CSC212 FG CS Dept, CCNY.
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.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
More STL Container Classes ECE Last Time Templates –Functions –Classes template void swap_val (VariableType &a, VariableType &b) { VariableType.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Standard Template Library a collection of useful tools.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
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.
Vectors Holds a set of elements, like an array
Standard Template Library (STL)
STACKS AND QUEUES UNIT 2 DS THROUGH C++.
What remains Topics Assignments Final exam
Elements are always copied when they are put into a container
Standard Template Library
Standard Template Library
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
Standard Template Library
Presentation transcript:

SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab.

SNU OOPSLA Lab. C++ Contents Standard Containers Sequences Sequence Adapters Associative Containers Almost Containers Defining a New Container

SNU OOPSLA Lab. C++ 1. Standard Containers Two kinds of containers Sequences Associative containers Not fully-developed standard containers Built-in arrays,strings,valarrays,bitsets

SNU OOPSLA Lab. C++ 1. Standard Containers Vector List Size Rep elementsExtra space rep Elements:

SNU OOPSLA Lab. C++ 1.Standard Containers Map String Rep... node Rep... Segment descriptors String segments: (key,value) pairs:

SNU OOPSLA Lab. C++ 1. Standard Containers Elements in a container are copies of the objects inserted by a copy constructor or an assignment Associative containers require that their elements can be ordered Sort(Ran first, Ran last) using < for comparison Sort(Ran first, Ran last, Cmp cmp) using cmp for comparison

SNU OOPSLA Lab. C++ 2.Sequences Vector,list,deque : fundamental sequences Stack,queue,priority_queue :container adapters or sequence adapters

SNU OOPSLA Lab. C++ 2. Sequences Vector Random-access iterators List Bidirectional iterators Types and operations like vector’s, except [], at(), capacity(), and reserve() Additional operations : slice, sort, merge,front, remove, unique

SNU OOPSLA Lab. C++ 2. Sequences Deque A double-ended queue Operations List-like efficiency at both ends Vector-like efficiency in the middle

SNU OOPSLA Lab. C++ 3. Sequence Adapters Provides a restricted interface to a container Stack Queue Priority Queue

SNU OOPSLA Lab. C++ 3. Sequence Adapters Stack Defined in template > class std::stack { protected: C c: public: typedef typename C::value_type value_type; typedef typename C::size_type size_type; typedef C container_type; explicit stack(const C& a = C()) : c(a){ } bool empty() const { return c.empty(); } size_type size() const {return c.size(); } value_type& top() {return c.back();} const value_type& top() const {return c.back(); } void push { const value_type& x) { c.push_back(x); } void pop() { c.pop_back(); } } pass a container as a template arg give back,push_back, pop_back their conventional names : top,push,pop

SNU OOPSLA Lab. C++ 3. Sequences Adapters Queue Defined in An interface to a container that allows an insertion of elements at the back() and the extraction of elements at the front() template > class std:queue{ … value_type& front(){ return c.front();} const value_type& front() const {return c.front();} value_type& back(){ return c.back(); } const value_type& back() const { return c.back(); } }

SNU OOPSLA Lab. C++ 3. Sequences Priority Queue Defined in A queue in which each element is given a priority that controls the order template,class Cmp=less > class std::priority_queue{ … explicit priority_queue(const Cmp& a1=Cmp(), const C& a2=C()) :c(a2),cmp(a1){ } priority_queue(In first, In last, const Cmp&=Cmp(),const C&=CC()); void pop(); } Compare elements using the < op by default return the largest element

SNU OOPSLA Lab. C++ 4. Associative Containers Generalization of the notion of an associative array Associative array Keeps pairs of values : (key, mapped value) Map,multimap Set, multiset

SNU OOPSLA Lab. C++ 4. Associative Containers Map A sequence of (unique key,value) pairs that provides for fast retrieval based on the key Bidirectional iterators Type value_type of a map : (key,value) pair mapped_type : the type of the mapped values Iteration Simply an iteration over a sequence of pair Subscripting Performs a lookup on the key and returns correponding value

SNU OOPSLA Lab. C++ 4. Associative Containers Comparisons By default, < (less than) key_comp(), value_comp() Map operation find() : find element with key k count() : find number of elements with key k lower_bound() : find first element with key k upper_bound() : find key element with key greater than k

SNU OOPSLA Lab. C++ 4. Associative Containers Multimap Is like a map,except that it allows duplicate keys The primary means of accessing multiple values with the same key :equal_range(),lower_bound,upper_bound() Set Can be seen as a map concerning only the keys Rely on a comparison op rather than equlity(==) Multiset A set that allows duplicate keys

SNU OOPSLA Lab. C++ 5. Almost Containers Hold elements, but lack some aspect of the standard container interface Built-in array, strings, valarrays, bitsets basic_string Provides subscripting, random-access iterators, and most of the notational conveniences of a container Valarray A vector for optimized numeric computation Provides many useful numeric operations

SNU OOPSLA Lab. C++ 5. Almost Containers Bitset A bitset is an array of N bits with binary conditions Ex) Constructors Constructed with default values from the bits in an unsigned long int, or from a string Bit Manipulation Operations Binary &(and),| (or), ^(exclusive or), >(logical right shift), set(),reset(),flip()... Built-in Arrays Supplies subscripting and random-access iterators in the form of ordinary pointers bitset :

SNU OOPSLA Lab. C++ 6. Defining a New Container A user can design additional containers to fit into the framework provided by the standard containers Hash_map A map with a hash function The difference between a map and a hash_map A map requires a < for its element type A hash_map requires an == and a hash function A first approximation of a hash_map Template, class EQ=equal_to, class A = allocator > class hash_map { //like map, except: typedef H Hasher; typedef EQ key_equal; hash_map(const T& dv=TT(),size_type n=101,const h& hf=H(),const EQ&=EQ()); … hash_map(In first, In last,const TT&dv = T(),size_type n=101,const H& hf=H()); }

SNU OOPSLA Lab. C++ 6.Defining a New Container The key operations The constructors The lookup(operator[]) The resize operation The operation removing an element Each Entry A key, a value, a pointer to the next entry with same hash value, and an erased bit Class hash_map{… struct Entry { key_type key; mapped_type val; Entry* next; bool erased; … } …. Key val e next...