Standard Template Library Programming paradigm: generic programming the decomposition of programs into components which may be developed separately and.

Slides:



Advertisements
Similar presentations
M The University Of Michigan Andrew M. Morgan EECS Lecture 22 Savitch Ch. 16 Intro To Standard Template Library STL Container Classes STL Iterators.
Advertisements

SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Vectors, lists and queues
Multimaps. Resources -- web For each new class, browse its methods These sites are richly linked, and contain indexes, examples, documents and other resources.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
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.
Standard Containers: Vectors
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
More on the STL vector list stack queue priority_queue.
Templates & STL Instructor: 小黑. Templates  Template serves as a class outline, from which specific classes are generated at compile time.  One template.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Standard Template Library. Homework List HW will be posted on webpage Due Nov 15.
Standard Template Library (STL) Overview – Part 1 Yngvi Bjornsson.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
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.
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.
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:
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
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
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.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
STL-Associative Containers. Associative Containers Sequence containers : "This is item 0, this is item 1, this is item 2…“ Associative containers : –
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)
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
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,
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.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000.
 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 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.
The Standard Template Library Container Classes Version 1.0.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
C++ Review STL CONTAINERS.
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.
1 The Standard Template Library Drozdek Section 3.7.
Vectors. Basics A vector is like an array, but more flexible A vector provides (constant time) random access to its elements A vector may be dynamically.
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.
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.
Object-Oriented Programming (OOP) Lecture No. 41
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>
Lists - I The List ADT.
Lists - I The List ADT.
Standard Template Library
Standard Template Library
Chapter 3 Lists, Stacks, and Queues
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Presentation transcript:

Standard Template Library Programming paradigm: generic programming the decomposition of programs into components which may be developed separately and combined arbitrarily, subject only to well-defined interfaces. lots of orthogonality

Components Containers Iterators Algorithms

Example 1 // Simple STL vector program from Ira Pohl, UC Santa Cruz #include using namespace std; int main () { vector v(100); // 100 is vector's size for (int i = 0; i < 100; ++i) v[i] = i; for (vector ::iterator p = v.begin(); p != v.end(); ++p) cout << *p << '\t'; cout << endl; }

Example 2 #include #include // list container #include // for accumulate using namespace std; // Using the list container int main() { double w[4] = { 0.9, 0.8, 88, }; list z; for (int i = 0; i < 4; ++i) z.push_front(w[i]); print(z); z.sort(); print(z); cout << "sum is " << accumulate(z.begin(), z.end(), 0.0) << endl; }

Containers Sequential – ordered by sequence Vector – dynamic array List – linked list Deque – double-ended queue Associative – use keys for ordering Sets – stores unique keys Multisets – allows multiple copies of key Maps – associates unique keys and values Multimaps – allows multiple copies of key

Common container interfaces Constructors Element access Element insertion Element deletion Destructor Iterators

Useful definitions and members CON::iterator CON::value_type c.begin() c.end() c.size() c.empty()

Useful sequence members c.insert(w_it, v) – inserts v before w_it c.insert(w_it,v,n) – inserts n copies of v before w_it c.insert(w_it,b_it,e_it) – inserts b_it to e_it before w_it c.erase(w_it) – erases element at w_it c.erase(b_it,e_it) – erases b_it to e_it

Useful Associative Container Definitions, Members ASSOC::key_type c.find(k) – returns iterator to element with key k or c.end() c.count(k) – returns number of elements with key k

Example 3 - Map //Associative Containers - looking up ages #include using namespace std; int main() { map > name_age; name_age["Pohl,Laura"] = 12; name_age["Dolsberry,Betty"] = 39; name_age["Pohl,Tanya"] = 14; cout << "Laura is " << name_age["Pohl,Laura"] << " years old." << endl; }

Example 4 - Multiset #include #include //used for both set and multiset #include using namespace std; enum vegetables { broccoli, tomato, carrot, lettuce, beet, radish, potato}; int main() { vector my_diet(100); vector ::iterator pos; vegetables veg; multiset > v_food; multiset >::iterator vpos; for (pos = my_diet.begin(); pos != my_diet.end(); ++pos) { *pos = static_cast (rand() % 7); v_food.insert(*pos); } for (veg = broccoli; veg <= potato; ++veg) cout << v_food.count(veg) << endl; }

Example 5- Container Adaptors //Adapt a stack from a vector #include using namespace std; int main() { stack > str_stack; string quote[3] = { “Let’s all visit\n", “Greensburg\n",“Bill Watts\n" }; for (int i = 0; i < 3; ++i) str_stack.push(quote[i]); while (!str_stack.empty()) { cout << str_stack.top(); str_stack.pop(); } }

Algorithms Sorting Nonmutating sequence algorithms Mutating sequence algorithms Numerical algorithms

Example 6 - Quicksort //Using sort() from STL #include using namespace std; const int N = 5; int main() { int d[N], i, *e = d + N; for (i = 0; i < N; ++i) d[i] = rand(); sort(d, e); for (i = 0; i < N; ++i) cout << d[i] << '\t'; }

Other useful sort-related algorithms binary_search(b,e,t) – true if t is found in b to e min_element(b,e) – returns iterator for minimum element in b to e includes(b1,e1,b2,e2) – returns true if second sequence is subset of first

Example 7 - find //Use of the find function #include using namespace std; int main() { string words[5] = { "my", "hop", "mop", "hope", "cope"}; string* where; where = find(words, words + 5, "hop"); cout << *++where << endl; //mop sort(words, words + 5); where = find(words, words + 5, "hop"); cout << *++where << endl; //hope }

Useful non-mutating sequence algorithms for_each(b,e,f) – applies function f to each value in b to e count_if(b,e,p,n) – returns in n the count of elements that make predicate p true equal(b1,e1,b2) – returns true if sequences match

Useful mutating sequence algorithms replace(b1,e1,t1,t2) – replaces t1 by t2 if t1 occurs in range b1 to e1 replace_if(b1,e1,p,t2) – replaces elements in b1 to e1 that satisfy predicate p with t2

Function objects Classes/structs that have operator() defined template struct notZero : public unary_function { bool operator()(Number x) const { return x != (Number) 0; }