Presentation is loading. Please wait.

Presentation is loading. Please wait.

Erasmus Exchange in Aristotel University

Similar presentations


Presentation on theme: "Erasmus Exchange in Aristotel University"— Presentation transcript:

1 Erasmus Exchange in Aristotel University
16-19 april 2019

2 Implementing parts of computer systems using Standard Library
Doru Anastasiu Popescu, associate professor, PhD Faculty of Sciences, Physical Education and Informatics Department of Mathematics and Informatics

3 Standard Library Building Offline Application Standard Library
Implementation: C++, Java, … classes, objects vector, queue, stack, set, …

4 Standard Library Building Online Application (Web application)
Standard PHP Library (SPL), Standard built-in objects (JavaScript) Cod Source: PHP, JavaScript, … classes, objects

5 Standard Template Library in C++
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).

6 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.

7 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.

8 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.

9 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 are of type 'pair' by default, where all the 'first' elements act as the unique keys, each associated with their 'second' value objects

10 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.

11 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).

12 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.

13 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.

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

15 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.

16 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

17 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

18 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

19 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

20 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

21 priority_queue 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

22 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 4

23 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;

24 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

25 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;

26 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

27 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;

28 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

29 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;

30 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;

31 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

32 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 T with the given numbers. Display maximum element of T. Display elements of T in the reverse order of reading. Example 4 56

33 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

34 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: C2: 2 4

35 PriorityQueue in Java Methods in PriorityQueue class:
boolean add(E element): This method inserts the specified element into this priority queue. public remove(): This method removes a single instance of the specified element from this queue, if it is present public poll(): This method retrieves and removes the head of this queue, or returns null if this queue is empty.

36 PriorityQueue in Java Methods in PriorityQueue class:
public peek(): This method retrieves, but does not remove, the head of this queue, or returns null if this queue is empty. Iterator iterator(): Returns an iterator over the elements in this queue. boolean contains(Object o): This method returns true if this queue contains the specified element void clear(): This method is used to remove all of the contents of the priority queue.

37 priority_queue Java. Example Create a priority queue with the names of 3 countries (Romania, Greece and Bulgaria), each country will be inserted n times. a) Show the element at the top of the priority queue without wiping it b) Wipe the first k in the priority queue. c) Delete Romania from the priority queue d) Check if the priority queue contains Greece

38 Java program priority_queue import java.util.*;
import java.util.Scanner; class ExamplePriorityQueue { public static void main(String args[]) // Creating empty priority queue PriorityQueue<String> pQueue = new PriorityQueue<String>(); // Adding items to the pQueue using add() Scanner cin=new Scanner(System.in); int n,x,i,k; n=cin.nextInt(); k=cin.nextInt();

39 Java program priority_queue for(i=1;i<=n;i++)
pQueue.add("Romania"); pQueue.add("Greece"); pQueue.add("Bulgaria"); // Printing the most priority element System.out.println("Head value using peek function:" + pQueue.peek()); // Printing all elements System.out.println("The queue elements:"); Iterator<String> itr = pQueue.iterator(); while (itr.hasNext()) System.out.println(itr.next());

40 Java program priority_queue
// Removing k top priority elements (or head) and // printing the modified pQueue using poll() for(i=1;i<=k;i++) pQueue.poll(); System.out.println("After removing " +k +" elements " + "with poll function:"); Iterator<String> itr2 = pQueue.iterator(); while (itr2.hasNext()) System.out.println(itr2.next());

41 Java program priority_queue // Removing using remove()
pQueue.remove("Romania"); System.out.println("after removing Romania with" + " remove function:"); Iterator<String> itr3 = pQueue.iterator(); while (itr3.hasNext()) System.out.println(itr3.next()); // Check if an element is present using contains() boolean b = pQueue.contains("Greece"); System.out.println ( "Priority queue contains Greece " + "or not?: " + b); }

42 Java priority_queue

43 JavaScript - Set Set objects are collections of values. You can iterate through the elements of a set in insertion order. A value in the Set may only occur once; it is unique in the Set's collection. new Set([iterable]); => If an iterable object is passed, all of its elements will be added to the new Set. If you don't specify this parameter, or its value is null, the new Set is empty. Properties: size

44 JavaScript – Set Method
add(value) - Appends a new element with the given value to the Set object. Returns the Set object clear() - Removes all elements from the Set object. delete(value) - Removes the element associated to the value and returns the value that has(value) - Returns a boolean asserting whether an element is present with the given value in the Setobject or not. …

45 Romania Greece Bulgaria
JavaScript.Example Let n be a natural number. Enter name of countries until there are n distinct name of countries. Build a set with the names of the countries and display it. Example Input n=3 Romania Greece Bulgaria Output Romania Greece Bulgaria

46 Script <html> <head> <script type="text/javascript"> s = new Set(); n=parseInt(prompt("Input a natural number n=")); alert("Input country names until they are n distinct countries"); do{ a=prompt("Input the name of a country"); s.add(a); }while(s.size!=n);

47 alert(" All name of a countrys:"); AllCountries=""; for (let item of s) AllCountries = AllCountries +item + " "; alert(AllCountries); </script> </head> <body> </body> </html>

48 PHP - array Until version 5, the most commonly used object was array. It allowed operations such as deleting, inserting, sorting, copying, etc. Starting with version 7 in PHP, a powerful library of classes with data structures in STL - C ++ have been introduced. This library is called SPL (Standard PHP Library). Here's an example of an array.

49 PHP - Example Let n and k natural numbers.
Build an array with the names of 3 countries (Romania, Greece and Bulgaria), each country will be inserted n times. Then display the array. Delete k country names and display the array. Order the name of the countries remaining in the array and display the array.

50 PHP - Script <?php $n=$_POST['n']; $k=$_POST['k']; $a = array(); $index = 0; for($i=1;$i<=$n;$i++) $a[++$index]="Romania"; $a[++$index]="Greece"; $a[++$index]="Bulgaria";

51 PHP - Script echo "Array content:"."<BR>"; foreach($a as $i=>$name) echo $i." ".$name."<BR>"; for($i=1;$i<=$k;$i++) array_pop($a); echo "Country names after deletion k countries:"."<BR>";

52 PHP - Script asort($a); echo "Country names after ordering:"."<BR>"; foreach($a as $i=>$name) echo $i." ".$name."<BR>"; ?>

53 HTML – for run PHP script
<html> <head></head> <body> <form action=" method="post"> n=<input type="text" name="n"><br> k=<input type="text" name="k"><br> <input type="submit" value="Script!"> </form> </body> </html>

54 Bibliography cppreference.com https://www.infoarena.ro/stl
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

55 Location of Presentation

56 WEB SITE: http://www.dopopan.ro
Thank you! Questions WEB SITE:


Download ppt "Erasmus Exchange in Aristotel University"

Similar presentations


Ads by Google