Programming in C++ Michal Brabec Petr Malý. Standard Template Library Containers - vector, map, set, deque, list Algorithms - copy, replace, sort, find.

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

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Data Structures Using C++ 2E
. 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.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
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.
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Templates and the STL.
Writing Your Own STL Container Ray Lischner
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
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)
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)
CSE 332: C++ STL containers Review: 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.
Iterators CS Chakrabarti What is an iterator?  Thus far, the only data structure over which we have iterated was the array for (int ix = 0;
Standard Template Library (STL) Chapter 4, General Summary Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics,
Data Structures Using C++1 Chapter 4 Standard Template Library (STL)
C++ Programming Part 2 Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
1 Iterators Good reference site:
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.
1 Chapter 1 C++ Templates Sections 1.6 and Templates Type-independent patterns that can work with multiple data types –Generic programming –Code.
Intro to the C++ STL Timmie Smith September 6, 2001.
Programming in C++ Michal Brabec Petr Malý. Class / Struct Class / Struct consists of: data members function members constructors destructor copy constructor.
Standard Template Library (STL) - Use Vector and Deque
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.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
Chapter 1 C++ Templates (Sections 1.6, 1.7). Templates Type-independent patterns that can work with multiple data types. Function Templates  These define.
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)
More STL Container Classes ECE Last Time Templates –Functions –Classes template void swap_val (VariableType &a, VariableType &b) { VariableType.
1 ENERGY 211 / CME 211 Lecture 7 October 6, 2008.
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.
Object-Oriented Programming (OOP) Lecture No. 42.
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.
CS212: Object Oriented Analysis and Design
COP 3530 Data Structures & Algorithms
Programming with ANSI C ++
Standard Template Library
Standard Template Library (STL)
STL Common tools for C++.
Starting Out with C++ Early Objects Eighth Edition
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Chapter 22: Standard Template Library (STL)
CS212: Object Oriented Analysis and Design
Generic Programming Karl Lieberherr 12/1/2018 Generic Programming.
Standard Template Library Model
STL Iterators Separating Container from Data Access.
Standard Template Library
Iterators and STL Containers
STL List.
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
STL List.
Standard Template Library
Presentation transcript:

Programming in C++ Michal Brabec Petr Malý

Standard Template Library Containers - vector, map, set, deque, list Algorithms - copy, replace, sort, find Iterators - Input, Output, Forward, Bidirectional, Random-access Function objects - equal_to, greater, less, logical_and

Containers vector, map, set, deque, list,... parametrized by another type(s) each container has specific member functions push_back, pop, front, back, insert,... common interface – iterators

Algorithms #include copy, replace, sort, find,... generic functions which manipulates with the container’s elements algorithms use iterators

Iterators Iterators are similar to pointers * dereference operator pointer arithmetic ContainerType::iterator, ContainerType::const_iterator myContainer1.begin(), begin(myContainer1); points to the first element of the container or end iterator myContainer1.end(), end(myContainer1); refers to the the past-the-end element should not be dereferenced

Loops std::vector myVec1 = { 10, 20, 30, 40, 50, 60, 70 }; for (int i = 0; i < myVec1.size(); ++i) std::cout << myVec1[i] << " "; // the type of i is std::vector ::const_iterator for (auto i = myVec1.begin(); i != myVec1.end(); ++i) std::cout << *i << " "; for (auto i : myVec1) std::cout << i << " "; std::for_each(myVec1.begin(), myVec1.end(), [](int i){ std::cout << i << " "; );

Iterator Categories categorypropertiesvalid expressions all categories copy-constructible, copy-assignable and destructible X b(a); b = a; Can be incremented ++a a++ Random Access Bidirectional Forward Input Supports equality/inequality comparisons a == b a != b Can be dereferenced as an rvalue *a a->m Output Can be dereferenced as an lvalue (only for mutable iterator types) *a = t *a++ = t default-constructible X a; X() Multi-pass: neither dereferencing nor incrementing affects dereferenceability { b=a; *a++; *b; } Can be decremented --a a-- *a-- Supports arithmetic operators + and - a + n n + a a - n a - b Supports inequality comparisons (, =) between iterators a b a = b Supports compound assignment operations += and -= a += n a -= n Supports offset dereference operator ([])a[n]

Task Create a global function remove_same Function gets two parameters: Container1 cnt1 – any STL container Container2 cnt2 – any STL container Function removes all elements which are contained in both objects

Task Use code from previous practicals Create a program which loads the complex numbers from the file The program stores the number ordered by their absolute value in other file Use STL functions / containers