STL. What is STL? Standard Templates Library Templates are best used for –Defining containers for storing data –Some kinds of algorithms that work the.

Slides:



Advertisements
Similar presentations
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Advertisements

Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Vectors, lists and queues
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Monday, 11/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Monday, 11/11/02  Questions? HW 04 due today at 5.  Today – Lists and an introduction to searching.
CSE 332: C++ Algorithms I The C++ Algorithm Libraries A standard collection of generic algorithms –Applicable to various types and containers E.g., sorting.
Beginning C++ Through Game Programming, Second Edition
Introduction to the STL
More on the STL vector list stack queue priority_queue.
Computer programming 1 Multidimensional Arrays, and STL containers: vectors and maps.
Templates & STL Instructor: 小黑. Templates  Template serves as a class outline, from which specific classes are generated at compile time.  One template.
OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Standard library types Practical session # 3 Software Engineering
Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Lecture 23 Today Standard Template Library Programs in: programs/p19 Bibliography: Textbook p.252,
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
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:
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
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
1 Today’s Objectives  Announcements The Final Exam will be on Monday, 31-Jul, at 6 p.m. – There is no alternate time and no makeup!  Intro to the Standard.
CSE 332: C++ STL algorithms C++ STL Algorithms Generic algorithms –Apply to a wide range of types E.g., sorting integers (long) or intervals (long, long)‏
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:
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Section 3.5, class notes Iterators Generic algorithms.
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.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
Ticket Booth Base Level 3 1. In the completed program, the ticket seller will: Select a venue from a menu of all venues. Select a show from a menu of.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
1 Associative Containers Ordered Ordered Unordered UnorderedSets Maps as sets of pairs Set API Ex: Sieve of Eratosthenes Ex: Sieve of EratosthenesImplementation.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
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.
Vectors CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
More STL Container Classes ECE Last Time Templates –Functions –Classes template void swap_val (VariableType &a, VariableType &b) { VariableType.
1 The Standard Template Library Drozdek Section 3.7.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
1.What are the differences between composition(“has a” relationship) and inheritance(“is a” relationship)? 2.What is a base class? 3.How to declare a derived.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer.
Lecture 7-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
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.
Generic Algorithms (TIC++V2:C6)
Lecture 7-2 : STL Iterators
Programming fundamentals 2 Chapter 1:Array
The C++ Algorithm Libraries
Collections Intro What is the STL? Templates, collections, & iterators
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Lecture 7-2 : STL Iterators
C++ Functions, Classes, and Templates
Elements are always copied when they are put into a container
STL: Traversing a Vector
CSE 303 Lecture 25 Loose Ends and Random Topics
Lecture 8-2 : STL Iterators and Algorithms
STL Библиотека стандартных шаблонов
Standard Template Library
Collections Intro What is the STL? Templates, collections, & iterators
Standard Template Library
An Introduction to STL.
Chapter 3 Lists, Stacks, and Queues
Presentation transcript:

STL

What is STL? Standard Templates Library Templates are best used for –Defining containers for storing data –Some kinds of algorithms that work the same on lots of data types Sorting etc. This is what STL provides Before implementing a template, check STL

STL in a glance STL Containers Sequence Vector List Associative Map Set IteratorsAlgorithms Search Sort (A non-exhaustive list)

STL Containers Manage a collection of elements Different implementations –Different APIs map provides access by key, vector doesn’t –Different operation complexities

Example: using a vector #include using namespace std; main() { vector v; v.push_back(“Sunday”); v.push_back(“Monday”); v.push_back(“Tuesday”); for(int i = 0; i < v.size(); i++) { cout << v[i] << endl; } Output Sunday Monday Tuesday

Example: iterators #include using namespace std; main() { vector v; //initialization omitted // for(int i = 0; i < v.size(); i++) { // cout << v[i] << endl; // } vector ::iterator iter; for (iter = v.begin(); iter != v.end(); ++iter){ cout << *iter << endl; } Output Sunday Monday Tuesday

Iterators Represent a location within collection Operators: * get the collection’s element at the represented location ==, != compare locations’ identity ++ move to the next position -- move to the previous position +=, -= jump several positions forward/backward Container may invalidate iterators if modified Use const_iterator to protect referenced element from accidental modification v.begin()(v.begin())++ v v.end() cout<<*(v.end()); //ERROR

Example: const iterators vector > v; … vector >::iterator iter; for (iter = v.begin(); iter != v.end(); ++iter) { cout << (*iter).size() << endl; // ok (*iter).push_back(“Hello”); //ok } vector >::const_iterator iter; for (iter = v.begin(); iter != v.end(); ++iter){ cout << (*iter).size() << endl; // ok (*iter).push_back(“Hello”); //error }

vector vs. list vector v; v.push_back(“Sunday”); … vector ::iterator iter; for (iter = v.begin(); iter != v.end(); ++iter){ cout << *iter << endl; } list v; v.push_back(“Sunday”); … list ::iterator iter; for (iter = v.begin(); iter != v.end(); ++iter){ cout << *iter << endl; }

vector vs. list (cont.) vector can do everything list can –And then some operator[] Why have list at all? –Operations have different cost –Which one is best depends on application

Example: map // Word frequencies -- using map #include using namespace std; int main() { map freq; // word->frequency map; //--- Read words from input stream while (cin >> word) { freq[word]++; } //--- Write the count and the word. map ::const_iterator iter; for (iter=freq.begin(); iter != freq.end(); ++iter) { cout second first << endl; } return 0; }//end main Source:

Algorithms #include using namespace std; int main() { vector v; for (int i=1; i<=7; ++i) { v.push_back(i); } for (int i=0; i<v.size(); ++i) { cout << v[i] << ' '; } cout<<endl; vector ::iterator p = find( v.begin(), v.end(), 1 ); if(p==v.end()) cout<<"not found\n"; else v.erase(p); for (int i=0; i<v.size(); ++i) { cout << v[i] << ' '; } cout<<endl; return 0; }

Algorithms #include using namespace std; void print(int i) { cout << i << ' '; } bool MoreThan1(int i) { return i > 1; } int main() { int a[5] = {5, 4, 1, 3, 2}; sort(&a[0], &a[5]); for_each(&a[0], &a[5], print); cout << '\n' << *( find_if(&a[2], &a[5], MoreThan1)); return 0; }