Erasmus Exchange in Ionian University

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.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
Standard Template Library (STL) Overview – Part 1 Yngvi Bjornsson.
Writing Your Own STL Container Ray Lischner
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced.
Data Structures Using C++ 2E
SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab.
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)
C++ STL CSCI 3110.
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
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.
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.
Data Structures for Midterm 2. C++ Data Structure Runtimes.
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.
C++ Review STL CONTAINERS.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
CSCI-383 Object-Oriented Programming & Design Lecture 30.
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.
Final Exam Review COP4530.
Object-Oriented Programming (OOP) Lecture No. 41
Lecture 35a OOP The STL Standard Template Library
Standard Template Library
CS212: Object Oriented Analysis and Design
Cpt S 122 – Data Structures Abstract Data Types
18 Chapter Stacks and Queues
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Programming with ANSI C ++
Standard Template Library
Data Structure Interview Question and Answers
Chapter 15 Lists Objectives
Standard Template Library (STL)
Basic Data Structures.
Csc 2720 Instructor: Zhuojun Duan
ENERGY 211 / CME 211 Lecture 19 November 3, 2008.
Introduction to Data Structure
What remains Topics Assignments Final exam
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
structures and their relationships." - Linus Torvalds
Basic Data Structures.
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
structures and their relationships." - Linus Torvalds
Object Oriented Programming COP3330 / CGS5409
Discussion section #2 HW1 questions?
Final Exam Review COP4530.
priority_queue<T>
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Lecture 8 : Intro. to STL (Standard Template Library)
Containers: Queue and List
STL Библиотека стандартных шаблонов
STL (Standard Template Library)
Standard Template Library
C++ Programming: chapter 10 – STL
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Standard Template Library
Erasmus Exchange in Aristotel University
structures and their relationships." - Linus Torvalds
A dictionary lookup mechanism
Standard Template Library
Presentation transcript:

Erasmus Exchange in Ionian University 30 april – 3 May 2018

Data Structures Stacks and queues in C++ (STL). Applications Doru Anastasiu Popescu, associate professor, PhD Faculty of Sciences, Physical Education and Informatics Department of Mathematics and Informatics

Standard Template Library The Standard Template Library (STL) is a software library for the C++ programming language that influenced many parts of the C++ Standard Library. The most important components are containers, iterators and algorithms. The STL provides a ready-made set of common classes for C++, such as containers and associative arrays, that can be used with any built-in type and with any user-defined type that supports some elementary operations (such as copying and assignment).

Standard Template Library STL algorithms are independent of containers, which significantly reduces the complexity of the library. The STL achieves its results through the use of templates. This approach provides compile-time polymorphism that is often more efficient than traditional run-time polymorphism. Modern C++ compilers are tuned to minimize any abstraction penalty arising from heavy use of the STL.

Standard Template Library The STL was created as the first library of generic algorithms and data structures for C++, with four ideas in mind: generic programming, abstractness without loss of efficiency, the Von Neumann computation model, and value semantics.

Containers The STL contains sequence containers and associative containers. The standard sequence containers include vector, deque, and list. The standard associative containers are set, multiset, map, multimap, hash_set, hash_map, hash_multiset and hash_multimap. There are also container adaptors queue, priority_queue, and stack, that are containers with specific interface, using other containers as implementation.

pair The pair container is a simple associative container consisting of a 2-tuple of data elements or objects, called 'first' and 'second', in that fixed order. The STL 'pair' can be assigned, copied and compared. The array of objects allocated in a map or hash_map are of type 'pair' by default, where all the 'first' elements act as the unique keys, each associated with their 'second' value objects

vector a dynamic array, like C array (i.e., capable of random access) with the ability to resize itself automatically when inserting or erasing an object. Inserting an element to the back of the vector at the end takes amortized constant time. Removing the last element takes only constant time, because no resizing happens. Inserting and erasing at the beginning or in the middle is linear in time. A specialization for type bool exists, which optimizes for space by storing bool values as bits.

list a double linked list; elements are not stored in contiguous memory. Opposite performance from a vector. Slow lookup and access (linear time), but once a position has been found, quick insertion and deletion (constant time).

set Sets are containers that store unique elements following a specific order. In a set, the value of an element also identifies it (the value is itself the key, of type T), and each value must be unique. The value of the elements in a set cannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container.

set Internally, the elements in a set are always sorted following a specific strict weak ordering criterion indicated by its internal comparison object (of type Compare). set containers are generally slower than unordered_set containers to access individual elements by their key, but they allow the direct iteration on subsets based on their order.

set Sets are typically implemented as binary search trees Methods: empty, size, clear, insert, erase, swap, count, find, key_comp, value_comp, …

deque a vector with insertion/erase at the beginning or end in amortized constant time, however lacking some guarantees on iterator validity after altering the deque.

stack Provides LIFO stack interface in terms of push/pop/top operations (the last inserted element is on top). Any sequence supporting operations back(), push_back(), and pop_back() can be used to instantiate stack (e.g. vector, list, and deque). #include <stack> stack<T> s; T = data type

Stack. Main methods Element access top accesses the top element Capacity empty checks whether the underlying container is empty size returns the number of elements Modifiers push inserts element at the top pop removes the top element swap swaps the contents Operators ==,!=,<,<=,>,>= lexicographically compares the values in the stack

queue Provides FIFO queue interface in terms of push/pop/front/back operations. Any sequence supporting operations front(), back(), push_back(), and pop_front() can be used to instantiate queue (e.g. list and deque). #include<queue> queue<T> q; T = data type

queue. Main methods Element access front access the first element back access the last element Capacity empty checks whether the underlying container is empty size returns the number of elements Modifiers push inserts element at the end pop removes the first element swap swaps the contents Operators ==,!=,<,<=,>,>= lexicographically compares the values in the queue

priority_queue Provides priority queue interface in terms of push/pop/top operations (the element with the highest priority is on top). Any random-access sequence supporting operations front(), push_back(), and pop_back() can be used to instantiate priority_queue (e.g. vector and deque). It is implemented using a heap. Elements should additionally support comparison (to determine which element has a higher priority and should be popped first). #include <queue> priority_queue<T> p; T=data type

priority_queue. Main methods Element access Top accesses the top element Capacity empty checks whether the underlying container is empty size returns the number of elements Modifiers push inserts element and sorts the underlying container pop removes the top element swap swaps the contents Operators ==,!=,<,<=,>,>= lexicographically compares the values in the priority_queue

Applications Stacks Let n, m - natural numbers with a maximum of 5 digits, m≤n. Write a C ++ program to solve the requirements: Build a stack s with numbers 1, 2, ..., n. Copy the stack in t. Display stack s. Delete components from stack t. Display the number of stack components t. Display stack t. Example n= 10 m=6 10 9 8 7 6 5 4 3 2 1 4 4 3 2 1

C++ program Stacks #include <iostream> #include <stack> using namespace std; int main() { stack<int> s, t; int n,m,i; cout<<"n=";cin>>n; cout<<"m=";cin>>m; cout<<"a) "; for(i=1;i<=n;i++) s.push(i); t=s; while(!s.empty()){ cout<<s.top()<<" "; s.pop(); } cout<<'\n'; for(i=1;i<=m;i++) t.pop(); cout<<"b) "<<t.size()<<'\n';; cout<<"c) "; while(!t.empty()){ cout<<t.top()<<" "; return 0;

Applications queue Let n- natural number with a maximum of 5 digits. Write a C ++ program to solve the requirements: Build queue c with the first n prime numbers. Copy the c queue in t. Display queue c. Delete n/2 components from the queue t. Display queue t. Example n= 7 2 3 5 7 11 13 17 7 11 13 17

C++ program queue #include <iostream> #include<queue> using namespace std; int prim(int k){ int i; if(k<2) return 0; for(i=2;i<=k/2;i++) if(k%i==0) return 1; } int main() { int n,k,nr; queue<int> c,t; cout<<"n=";cin>>n; cout<<"a) "; k=2;nr=0; while(nr<n){ if(prim(k)){ c.push(k); nr++; } k++; t=c; nr=c.size(); for(k=1;k<=nr;k++){ cout<<c.front()<<" "; c.pop(); cout<<'\n'; cout<<"b) "; for(k=1;k<=nr/2;k++) t.pop(); while(!t.empty()){ cout<<t.front()<<" "; } return 0;

Applications priority_queue Let n- natural number with a maximum of 4 digits and n natural numbers. Write a C ++ program to solve the requirements: Build a priority queue c with the given numbers. Copy the stack in t. Display priority queue c . Delete n/2 components priority queue t. Display priority queue t. Example 7 88 34 2 56 100 3 14 100 88 56 34 14 3 2 34 14 3 2

C++ program priority_queue #include <iostream> #include <queue> using namespace std; int main() { priority_queue<int> c,t; int i,n,x,nr; cin>>n; for(i=1;i<=n;i++){ cin>>x; c.push(x); } cout<<"a) "; t=c; nr=c.size(); for(i=1;i<=nr;i++){ cout<<c.top()<<" "; c.pop(); } for(i=1;i<=nr/2;i++) t.pop(); cout<<'\n'; cout<<"b) "; while(!t.empty()){ cout<<t.top()<<" "; return 0;

Applications Set, queue It gives an undirected graph with n vertices and m edges. Visit the node of the graph starting from node r using BFS algorithm (breadth first search). Example graph.in graph.out 7 8 4 4 5 4 1 6 1 1 3 1 7 6 7 7 3 2 7 4 1 5 3 6 7 2

Applications set, queue #include <fstream> #include <queue> #include <set> using namespace std; ifstream fin("graph.in"); ofstream fout("graph.out"); int main() { int n,m,i,j,k,r; //read data fin >> n >> m>> r; // n = number of vertex, m = number of edge, r is start vertex set <int> A[n+1]; bool v[n+1]; // vector for visiting vertex for(i=1;i<=n;i++) v[i]=false;

Applications set, queue for(k=1;k<=m; k++){ //read edge [i,j] fin>>i>>j; A[i].insert(j); A[j].insert(i); } // Q is queue with vertex (nods) queue <int> Q,Q1; Q.push(r);Q1.push(r); v[r]=true; while(!Q.empty()){ k=Q.front(); Q.pop(); for(i=1;i<=n;i++) if(!v[i] && A[k].find(i)!=A[k].end()) { v[i]=true; Q.push(i); Q1.push(i); //display vertex from Q1 while(!Q1.empty()){ fout<<Q1.front()<<" "; Q1.pop(); } return 0;

Proposed problems stack 1. Let n- natural number with a maximum of 4 digits and n natural numbers. Write a C ++ program to solve the requirements: Build a stack S with the given numbers. Display elements of S. Display elements of S in order of reading. Example 4 89 4 56 7 7 56 4 89

Proposed problems queue 2. Let n- natural numbers with a maximum of 4 digits and n natural numbers. Write a C ++ program to solve the requirements: Build a queue Q with the given numbers. Display maximum element of Q. Display elements of Q in in the reverse order of reading. Example 4 9 4 56 7 56 7 56 4 9

Proposed problems set, queue It gives an undirected graph with n vertices and m edges. Visit the node of the graph starting from node r using DFS algorithm (Depth First Search). Example graph.in graph.out 7 8 4 4 5 4 1 6 1 1 3 1 7 6 7 7 3 2 7 4 1 3 7 2 6 5

Proposed problems set, stack, queue It gives an undirected graph with n vertices and m edges.  Using the DFS or BFS algorithm find the conex components in the graph. Example graph.in graph.out 6 5 1 5 1 3 2 4 3 5 6 3 C1: 1 5 3 6 C2: 2 4

Bibliography cppreference.com https://www.infoarena.ro/stl http://www.cs.wustl.edu/~schmidt/PDF/stl4.pdf https://www.cs.rpi.edu/academics/courses/spring14/ds/lectures/20_priority_queues.pdf Ulrich Breymann , Designing Components with the C++ STL, ADDISON –WESLEY, 2008 Mark Nelson: C++ Programmer’s Guide to the Standard Template Library, IDG Books, 1995 Graham Glass & Brett Schuchert: The STL Primer, Prentice Hall, 1996 David R. Musser & Atul Sahni: STL Tutorial and Reference Guide, Addison-Wesley, 1996

WEB SITE: http://www.dopopan.ro Thank you! Questions Email: dopopan@yahoo.com WEB SITE: http://www.dopopan.ro