CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:

Slides:



Advertisements
Similar presentations
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
Advertisements

Multimaps. Resources -- web For each new class, browse its methods These sites are richly linked, and contain indexes, examples, documents and other resources.
CSE 332: C++ Algorithms I The C++ Algorithm Libraries A standard collection of generic algorithms –Applicable to various types and containers E.g., sorting.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
What is generic programming? The essence of the generic programming approach is concept development: systematic classification of computing components.
. 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.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11a. The Vector Class.
Basic C++ Sequential Container Features
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.
CSE 332: C++ Associative Containers II Associative Containers’ Associated Types Associative containers declare additional types –A key_type gives the type.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Templates and the STL.
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
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’
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,
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:
Standard Template Library There are 3 key components in the STL –Containers –Iterators –Algorithms Promote reuse More debugged May be more efficient.
Data Structures Using C++ 2E
Containers Overview and Class Vector
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
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.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
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.
Software Design 1.1 Tapestry classes -> STL l What’s the difference between tvector and vector  Safety and the kitchen sink What happens with t[21] on.
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.
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,
CSE 332: C++ STL containers Intro to the C++ Standard Template Library (STL)‏ The STL is a collection of related software elements –Containers Data structures:
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.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
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.
CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000.
Overview of C++ Templates
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.
CS 403: Programming Languages Lecture 24 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
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.
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.
C++ Review STL CONTAINERS.
CSE 332: C++ STL iterators What is an Iterator? An iterator must be able to do 2 main things –Point to the start of a range of elements (in a container)
STL Associative Containers navigating by key. Pair Class aggregates values of two, possibly different, types used in associative containers defined in.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Object-Oriented Programming (OOP) Lecture No. 41
Motivation for Generic Programming in C++
Standard Template Library (STL)
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Standard Template Library
Lecture 8 : Intro. to STL (Standard Template Library)
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.
Presentation transcript:

CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures: store values according to a specific organization –Iterators Variables used to give flexible access to the values in a container –Algorithms Functions that use iterators to access values in containers Perform computations that modify values, or creates new ones –Function objects Encapsulate a function as an object, use to modify an algorithm The STL makes use of most of what we’ve covered –Functions, classes, structs –Extensive use of function and class templates, concepts –Typedefs, traits, and associated types

CSE 332: C++ STL containers Intro to C++ STL Containers Goals –See how STL containers are implemented –See how differences in implementation affect use We’ll cover several kinds –Focus on template concepts –And how each container’s concept relates to its implementation Example to the left prints v[0] is 1 v[1] is 2 v[2] is 4 #include using namespace std; int main (int, char *[]) { vector v; // This would be asking for trouble.... // v[0] = 1; v[1] = 2; v[2] = 4; //... but this works fine... v.push_back (1); v.push_back (2); v.push_back (4); //... and now this is ok... for (size_t s = 0; s < v.size(); ++s) { cout << "v[" << s << "] is " << v[s] << endl; } return 0; }

CSE 332: C++ STL containers template struct block{ typedef T value_type; // basic associated types typedef const T const_value_type; typedef value_type * pointer; typedef value_type & reference; typedef const_value_type* const_pointer; typedef const_value_type & const_reference; typedef size_t size_type; …… typedef pointer iterator; // iterator associated types typedef const_pointer const_iterator; …… }; From Matthew H. Austern, “Generic Programming and the STL” Example: Making a STL Container from an Array

CSE 332: C++ STL containers template struct block{ …… iterator begin() {return data;} // iterator accessors iterator end() {return data+N;} const_iterator begin() const {return data;} const_iterator end() const {return data+N;} …… reference operator [ ] (size_type n) // container interface {return data[n];} const_reference operator [ ] (size_type n) const {return data[n];} size_type size() const {return N;} T data [N]; // contained elements }; Array-based STL Container Example, Continued

CSE 332: C++ STL containers Basic Requirements for an STL Container Contains elements of a parameterized type –Sets/ranges of contained elements cannot overlap –Location cannot belong to more than one container Adding and copying elements is by value –Container owns the elements Container’s destructor destroys what it contains Provides interfaces to contained values –Functions to add/remove values, e.g., push_back –Operations, e.g. operator[] for random access –Should obey the “principle of least surprise” For example a linked list should not provide []

CSE 332: C++ STL containers Containers Must Also Work with Iterators Declare appropriate iterator types (traits) typedef pointer iterator; typedef const_pointer const_iterator; Provide iterator accessor/factory methods iterator begin() {return data;} iterator end() {return data+N;} const_iterator begin() const {return data;} const_iterator end() const {return data+N;} iterator rbegin() {return data+N-1;} iterator rend() {return data-1;} const_iterator rbegin() const {return data+N-1;} const_iterator rend() const {return data-1;} More about iterators next lecture

CSE 332: C++ STL containers Container Forward Container Reversible Container Random Access Container Sequence Front Insertion Sequence Back Insertion Sequence Container Simple Associative Container Pair Associative container Unique Associative Container Sorted Associative Container Multiple Associative Container Hashed Associative Container Hierarchy of STL Container Concepts Associative From: Matthew H. Austern, “Generic Programming and the STL”

CSE 332: C++ STL containers Container Forward Container Reversible Container Random Access Container Sequence Front Insertion Sequence Back Insertion Sequence General Container Concepts refined by models vector deque list Notice containers can have multiple classifications –Useful to look at differences between data structures! –Back vs. front insertion –Forward vs. reversible vs. random access More general concepts higher in the hierarchy More specific concepts appear farther down slist

CSE 332: C++ STL containers Container: Top of its Concept Hierarchy Valid ExpressionsComplexity –Copy constructor X(a) linear –Copy constructor X b(a) linear –Assignment operator b = a linear –Destructor a.~X() linear –Beginning of range a.begin() constant –End of range a.end() constant –Size a.size() container-dependent –Maximum size a.max_size() constant –Empty a.empty() constant –Swap a.swap(b) linear Container

CSE 332: C++ STL containers Forward Container Additional ExpressionsComplexity –Equality a == b linear –Inequality a != b linear –Less than a < b linear –Greater than a > b linear –Less than or equal a <= b linear –Greater than or equal a >= b linear Container Forward Container vector deque list slist

CSE 332: C++ STL containers Reversible Container Additional Expressions Complexity –Beginning of reverse range a.rbegin() constant –End of reverse range a.rend() constant vector deque list Container Forward Container Reversible Container

CSE 332: C++ STL containers Random Access Container Additional ExpressionsComplexity –Element access a[n] constant Container Forward Container Reversible Container Random Access Container vector deque

CSE 332: C++ STL containers Sequence Additional ExpressionsComplexity –Fill constructor X(n,t) linear –Fill constructor X(n) linear –Default constructor X() linear –Range constructor X(i,j) linear –Insert a.insert(p,t) sequence-dependent –Fill insert a.insert(p,n,t) linear –Range insert a.insert(p,i,j) linear –Erase a.erase(p) sequence-dependent –Range erase a.erase(p,q) linear –Front a.front() constant Container Forward Container Sequence vector deque listslist

CSE 332: C++ STL containers Front Insertion Sequence Additional ExpressionsComplexity –Push front a.push_front(t) constant –Pop front a.pop_front(t) constant Container Forward Container Sequence Front Insertion Sequence deque list slist

CSE 332: C++ STL containers Back Insertion Sequence Additional ExpressionsComplexity –Back a.back() constant –Push back a.push_back(t) constant –Pop back a.pop_back(t) constant vector deque list Container Forward Container Sequence Back Insertion Sequence

CSE 332: C++ STL containers Container Simple Associative Container Pair Associative container Unique Associative Container Sorted Associative Container Multiple Associative Container (Hashed Associative Container) Associative Container Concepts Associative Container Forward Container set multiset map multimap (hash_set) (hash_multiset) (hash_map) (hash_multimap) refined by models

CSE 332: C++ STL containers Associative Container Additional Expressions Complexity –Default constructor X () constant –Default constructor X a; constant –Find a.find(k) logarithmic –Count a.count(k) O(log(size())+count(k)) –Equal range a.equal_range(k)) logarithmic –Erase key a.erase(k) O(log(size())+count(k)) –Erase element a.erase(p) constant –Erase range a.erase(p,q) O(log(size())+count(k)) set multiset map multimap (hash_set) (hash_multiset) (hash_map)(hash_multimap)

CSE 332: C++ STL containers Unique Associative Container Additional ExpressionsComplexity –Range constructor X a(i,j) linear –Insert element a.insert(t) logarithmic –Insert range a.insert(i,j) O(Nlog(size()+N)) setmap (hash_set) (hash_map)

CSE 332: C++ STL containers Multiple Associative Container Additional Expressions Complexity –Range constructor X a(i,j) linear –Insert element a.insert(t) logarithmic –Insert range a.insert(i,j) O(Nlog(size()+N)) multisetmultimap (hash_multiset) (hash_multimap)

CSE 332: C++ STL containers Sorted Associative Container Additional Expressions Complexity –Default constructors X (); X a; constant –Default constructor with comparator X a(c) constant –Range constructor X(i,j) O(NlogN) –Range constructor w/ comparator X a(i,j,c) O(NlogN) –Key comparison a.key_comp() constant –Value comparison a.value_comp() constant –Lower bound a.lower_bound(k) logarithmic –Upper bound a.upper_bound(k) logarithmic –Equal range a.equal_range(k) logarithmic –Insert with hint a.insert(p,t) logarithmic set multiset map multimap

CSE 332: C++ STL containers (Hashed Associative Container) Additional Expressions Complexity –Default constructors X (); X a; constant –Default constructor with bucket count X a(n) constant –Default constructor with hash function X a(n,h) constant –Default constructor with key equality X a(n,h,k) constant –Range constructor X a(i,j) linear –Range constructor with bucket count X a(i,j,n) linear –Range constructor w/ hash fxn X a(i,j,n,h) linear –Range constructor w/ key eq X a(i,j,n,h,k) linear –Hash function a.hash_funct() constant –Key equality a.key_eq() constant –Bucket count a.bucket_count() constant –Resize a.resize() linear (hash_set)(hash_multiset) (hash_map)(hash_multimap)