Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab www.ai2lab.org Short Course on Programming in C/C++

Slides:



Advertisements
Similar presentations
Data Structures Using C++ 2E
Advertisements

Beginning C++ Through Game Programming, Second Edition
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
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.
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.
OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
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.
Data Structures Using C++1 Chapter 13 Standard Template Library (STL) II.
Templates and the STL.
Lecture 23 Today Standard Template Library Programs in: programs/p19 Bibliography: Textbook p.252,
Writing Your Own STL Container Ray Lischner
Standard Template Library Programming paradigm: generic programming the decomposition of programs into components which may be developed separately and.
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
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.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
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)
Data Structures Using C++ 2E Chapter 13 Standard Template Library (STL) II.
C++ STL CSCI 3110.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
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,
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.
Chapter 22 STL Containers §22.1 STL Basics §22.2 STL Iterators §22.3 Sequence Containers §22.4 Associative Containers §22.5 Container Adapters.
 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.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
Lecture 7 : Intro. to STL (Standard Template Library)
Template is a declaration (similar to class declaration), which deals with generic types. Templates are evaluated to produce concrete classes when a template-base.
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.
The Standard Template Library Container Classes Version 1.0.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objective  Standard Template Library.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues.
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.
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.
Programming in C++ Michal Brabec Petr Malý. Standard Template Library Containers - vector, map, set, deque, list Algorithms - copy, replace, sort, find.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
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.
Templates 3 Templates and type parameters The basic idea templates is simple: we can make code depend on parameters, so that it can be used in different.
Object-Oriented Programming (OOP) Lecture No. 41
CS212: Object Oriented Analysis and Design
Regarding homework 9 Many low grades
Exceptions, Templates, and the Standard Template Library (STL)
Standard Template Library (STL)
Starting Out with C++ Early Objects Eighth Edition
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Chapter 22: Standard Template Library (STL)
Miscellaneous Stuff Which there wasn’t enough time to cover
CS212: Object Oriented Analysis and Design
priority_queue<T>
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.
Standard Template Library
Presentation transcript:

Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++ Organized by Onur Pekcan Contributor Selim Temizer Instructor Hasan Yılmaz

Week 3 – Lecture2 Today We will cover; Standard Template Library (cont.)  Associative Containers  set, multi-set  map, multi-map  Operations/Utilities  Iterator  Algorithm 2

Associative Containers  set, multi-set  map, multi-map 3

set Unordered sets are containers that store unique elements in no particular order, and which allow for fast retrieval of individual elements based on their value.  (constructor)Construct unordered_set (public member function) (constructor)  (destructor)Destroy unordered set (public member function) (destructor)  operator=Assign content (public member function) operator=  emptyTest whether container is empty (public member function) empty  sizeReturn container size (public member function) size  max_sizeReturn maximum size (public member function) max_size  beginReturn iterator to beginning (public member type) begin  endReturn iterator to end (public member type) end  cbeginReturn const_iterator to beginning(public member function) cbegin  cendReturn const_iterator to end (public member function) cend 4

multi-set Unordered multisets are containers that store elements in no particular order, allowing fast retrieval of individual elements based on their value, much like unordered_set containers, but allowing different elements to have equivalent values.unordered_set  (constructor)Construct unordered_multiset (public member function) (constructor)  (destructor)Destroy unordered multiset (public member function) (destructor)  operator=Assign content (public member function) operator=  emptyTest whether container is empty (public member function) empty  sizeReturn container size (public member function) size  max_sizeReturn maximum size (public member function) max_size  beginReturn iterator to beginning (public member type) begin  endReturn iterator to end (public member type) end  cbeginReturn const_iterator to beginning (public member type) cbegin  cendReturn const_iterator to end (public member type) cend 5

map Unordered maps are associative containers that store elements formed by the combination of a key value and a mapped value, and which allows for fast retrieval of individual elements based on their keys.  (constructor)Construct unordered_map (public member function) (constructor)  (destructor)Destroy unordered map (public member function) (destructor)  operator=Assign content (public member function) operator=  emptyTest whether container is empty (public member function) empty  sizeReturn container size (public member function) size  max_sizeReturn maximum size (public member function) max_size  beginReturn iterator to beginning (public member function) begin  endReturn iterator to end (public member function) end  cbeginReturn const_iterator to beginning (public member function) cbegin  cendReturn const_iterator to end (public member function) cend 6

multi-map Unordered multimaps are associative containers that store elements formed by the combination of a key value and a mapped value, much like unordered_map containers, but allowing different elements to have equivalent keys.unordered_map  (constructor)Construct unordered_multimap (public member function) (constructor)  (destructor)Destroy unordered multimap (public member function) (destructor)  operator=Assign content (public member function) emptyTest whether container is empty (public member function) operator= empty  sizeReturn container size (public member function) size  max_sizeReturn maximum size (public member function) beginReturn iterator to beginning (public member type) max_size begin  endReturn iterator to end (public member type) end  cbeginReturn const_iterator to beginning (public member function) cbegin  cendReturn const_iterator to end (public member function) cend 7

Operations/Utilities  Iterator  Algorithm 8

iterator Iterator definitions In C++, an iterator is any object that, pointing to some element in a range of elements (such as an array or a container), has the ability to iterate through the elements of that range using a set of operators (at least, the increment (++) and dereference (*) operators). container The most obvious form of iterator is a pointer: A pointer can point to elements in an array, and can iterate through them using the increment operator (++). But other forms of iterators exist. For example, each container type (such as a vector) has a specific iterator type designed to iterate through its elements in an efficient way.container vector 9

iterator Notice that while a pointer is a form of iterator, not all iterators have the same functionality a pointer has; To distinguish between the requirements an iterator shall have for a specific algorithm, five different iterator categories exist: In this graph, each iterator category implements the functionalities of all categories to its right: 10 RandomAccess  Bidirectional  Forward  Input  Output

iterator Input and output iterators are the most limited types of iterators, specialized in performing only sequential input or output operations. Inputoutput Forward iterators have all the functionality of input and output iterators, although they are limited to one direction in which to iterate through a range. Forward iteratorsinputoutput Bidirectional iterators can be iterated through in both directions. All standard containers support at least bidirectional iterators types. Bidirectional iterators standard containers Random access iterators implement all the functionalities of bidirectional iterators, plus, they have the ability to access ranges non-sequentially: offsets can be directly applied to these iterators without iterating through all the elements in between. This provides these iterators with the same functionality as standard pointers (pointers are iterators of this category). Random access iteratorsbidirectional iterators 11

Example 1 // advance example #include using namespace std; int main () { list mylist; for (int i=0; i<10; i++) mylist.push_back (i*10); list ::iterator it = mylist.begin(); advance (it,5); cout << "The sixth element in mylist is: " << *it << endl; return 0; } 12

algorithm The header defines a collection of functions especially designed to be used on ranges of elements. A range is any sequence of objects that can be accessed through iterators or pointers, such as an array or an instance of some of the STL containers. Notice though, that algorithms operate through iterators directly on the values, not affecting in any way the structure of any possible container (it never affects the size or storage allocation of the container).STL containers 13

algorithm  for_eachApply function to range (template function) for_each  FindFind value in range (function template) Find  SwapExchange values of two objects (function template) Swap  swap_rangesExchange values of two ranges (function template) swap_ranges  ReplaceReplace value in range (function template) Replace  RemoveRemove value from range (function template ) Remove  SortSort elements in range (function template) Sort  MergeMerge sorted ranges (function template) Merge  MinReturn the lesser of two arguments (function template ) Min  MaxReturn the greater of two arguments(function template ) Max  min_elementReturn smallest element in range (function template) min_element  max_elementReturn largest element in range (function template ) max_element 14

Example 2 // for_each example #include using namespace std; void myfunction (int i) { cout << " " << i; } struct myclass { void operator() (int i) {cout << " " << i;} } myobject; int main () { vector myvector; myvector.push_back(10); myvector.push_back(20); myvector.push_back(30);//push 10,20,30 cout << "myvector contains:"; for_each (myvector.begin(), myvector.end(), myfunction); // or: cout << "\nmyvector contains:"; for_each (myvector.begin(), myvector.end(), myobject); cout << endl; return 0; } 15