1 / 34

Erasmus Exchange in Ionian University 30 april – 3 May 2018

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.

arthursmith
Download Presentation

Erasmus Exchange in Ionian University 30 april – 3 May 2018

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Erasmus Exchange in Ionian University 30 april – 3 May 2018

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

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

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

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

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

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

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

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

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

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

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

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

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

  15. Stack. Main methods Element access • topaccesses the top element Capacity • emptychecks whether the underlying container is empty • sizereturns the number of elements Modifiers • pushinserts element at the top • popremoves the top element • swapswaps the contents Operators • ==,!=,<,<=,>,>=lexicographically compares the values in the stack

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

  17. queue. Main methods Element access • frontaccess the first element • backaccess the last element Capacity • emptychecks whether the underlying container is empty • sizereturns the number of elements Modifiers • pushinserts element at the end • popremoves the first element • swapswaps the contents Operators • ==,!=,<,<=,>,>=lexicographically compares the values in the queue

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

  19. priority_queue. Main methods Element access • Topaccesses the top element Capacity • emptychecks whether the underlying container is empty • sizereturns the number of elements Modifiers • pushinserts element and sorts the underlying container • popremoves the top element • swapswaps the contents Operators • ==,!=,<,<=,>,>=lexicographically compares the values in the priority_queue

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

  21. 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()<<" "; t.pop(); } return 0; }

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

  23. 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 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()<<" "; t.pop(); } return 0; }

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

  25. 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()<<" "; t.pop(); } return 0; }

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

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

  28. 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; }

  29. 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 • 89 4 56 7

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

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

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

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

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

More Related