Шаблоны.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

The Stack Data Structure. Classic structure What is a Stack? An abstract data type in which accesses are made at only one end Last In First Out (LIFO)
Data Structure Lecture-5
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
C++ Programming Languages
C Intro.
Rossella Lau Lecture 12, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 12: Stack and Expression Evaluation  Stack basic.
The Stack Data Structure Mugurel Ionu Andreica Spring 2012.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 7 Ming Li Department of.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
1 Overloading functions C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define.
Built into qsort is a function that can swap two given array elements.
CS 1031 C++: Object-Oriented Programming Classes and Objects Template classes Operator Overloading Inheritance Polymorphism.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
CS 1031 Final Touches: Multiple-File Programs, Inheritance, Templates Multiple-File Programs –header files –implementation files –main-program file) Inheritance.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Lecture 17 Templates, Part I. What is a Template? In short, it’s a way to define generic functionality on parameters without needing to declare their.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
CS 162 Intro to Programming II Bubble Sort 1. Compare adjacent elements. If the first is greater than the second, swap them. Do this for each pair of.
Elementary C++. Procedural Programming Split your problem into simpler parts then solve each part separately Recognize common parts and solve them only.
Chapter 3 Templates Saurav Karmakar Spring Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates.
Exchange sort (Bubblesort) compares two consecutive items in the list, and swap them if they are out of order. for (i=1;i=i;j--) if.
Templates Where the TYPE is generic. Templates for functions Used when the you want to perform the same operation on different data types. The definition.
STACK Data Structure
Properties: -The value in each node is greater than all values in the node’s subtrees -Complete tree! (fills up from left to right) Max Heap.
The Linked List Data Structure Mugurel Ionu Andreica Spring 2012.
Templates. C++ 2 Outline Function templates  Function template definition  Function template overloading Class templates  Class template definition.
C++ Templates 1. Why Use Templates? C++ requires variables, functions, classes etc with specific data types. However, many algorithms (quicksort for example)
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
Copyright © Curt Hill Generic Functions Separating Data Type from Logic.
CPSC 252 Templatization Page 1 Templatization In CPSC152, we saw a class vector in which we could specify the type of values that are stored: vector intData(
REEM ALAMER REVISION FOR C LANGUAGE COURSE. OUTPUTS int main (void) { int C1, C2; int *p1, *p2; C1 = 8; p1 = &C1; C2 = *p1 / 2 + 5; p2 = &C2; printf ("C1.
C++ Lesson 1.
Asst.Prof.Dr. Tayfun ÖZGÜR
C++: Object-Oriented Programming
Data Structures and Algorithms
Function Overloading, Templated Functions, Templated Classes, Pointers to Functions Horton pp. 247 – 254 (function overloading and templated functions)
Priority Queues Sections 6.1 to 6.5.
Templates.
Linked List Yumei Huo Department of Computer Science
Reserved Words.
مبانی کامپیوتر و برنامه سازی
Object-Oriented Programming (OOP) Lecture No. 32
Chapter 16-2 Linked Structures
סוגי נתונים מופשטים (Abstract Data Type)
Introduction to Programming
Introduction to Programming
CSC 143 Stacks [Chapter 6].
Object-Oriented Programming (OOP) Lecture No. 37
Default Arguments.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Jordi Cortadella and Jordi Petit Department of Computer Science
C++: Object-Oriented Programming
CS150 Introduction to Computer Science 1
Stack.
Template.
Lab4 problems More about templates Some STL
C++ Templates An Introduction to Generic Programming.
Object-Oriented Programming (OOP) Lecture No. 34
Abstract Data Types Stacks CSCI 240
5.3 Implementing a Stack Chapter 5 – The Stack.
Presentation transcript:

Шаблоны

ШАБЛОНЫ ФУНКЦИЙ

SWAP void swap(int &x, int &y) { int z=x; x = y; y = z; }

void swap(char &x, char &y){ int temp = x; x = y; y = z; } void swap(int &x, int &y){ void swap(float &x, float &y){ void swap(double &x, double &y){

ШАБЛОН ФУНКЦИИ template < typename type1, typename type1 > type1 foo(type1 x, type2 y) { /*тело функции*/ } //template – служебное слово, что это шаблон //typename– служебное слово, обозначающее параметр

SWAP template <typename type> void swap(type x, type y){ type temp = x; x = y; y = z; }

void mSort(int. first, int. last){ for(int. i=first; i void mSort(int *first, int * last){ for(int * i=first; i!=last-1; ++i) for(int * j=i; j!=last-1; ++j) if(*j<*(j+1)) swap(*j,*(j+1)); }

template <typename T> void mSort(T *first, T * last){ for(T * i=first; i!=last-1; ++i) for(T * j=i; j!=last-1; ++j) if(*j<*(j+1)) swap(*j,*(j+1)); } int main(){ int n; cin>>n; int * A = new int[n]; double * A = new double[n]; for(int i=0; i<n; ++i) i[A]=rand()%100/10.1; sort(A,A+n); cout<<i[A]<<" "; }

ШАБЛОНЫ СТРУКТУР

struct pair{ int first; itn second; };

PAIR template <typename T1, typename T2> struct pair{ T1 first; T2 second; };

PAIR template <typename T1, typename T2> struct mPair{ T1 first; T2 second; }; int main(){ mPair<int,int> pInt; pInt.first=1,pInt.second=2; mPair<double,string> p; p.first=12.44; p.second="Krya"; }

PAIR template <typename T1, typename T2> mPair<T1,T2> makePair(T1 first,T2 second){ mPair<T1,T2> temp; temp.first=first; temp.second=second; return temp; } int main(){ int n; cin>>n; mPair<int, int> * l = new mPair<int, int>[n]; for(int i=0; i<n; ++i) l[i]=makePair(rand()%100,rand()%100);

PAIR int main(){ int n; cin>>n; mPair<int, int> * l = new mPair<int, int>[n]; for(int i=0; i<n; ++i) l[i]=makePair(rand()%10,rand()%10); sort(l,l+n); } /* [Error] no match for 'operator<' (operand types are 'mPair<int, int>' and 'mPair<int, int>') */

< template <typename T1, typename T2> struct mPair{ T1 first; T2 second; bool operator < (mPair b){ return first!=b.first ? first<b.first : second<b.second; } };

Comparator bool cmp(mPair<int, int> a, mPair<int, int> b){ return a.first!=b.first ? a.first<b.first : a.second<b.second; } template <typename T> void mSort(T *first, T * last, bool (*cmp) (T a, T b)){ for(T * i=first; i!=last-1; ++i) for(T * j=i; j!=last-1; ++j) if(cmp(*j,*(j+1))) swap(*j,*(j+1));

int main(){ int n; cin>>n; mPair<int, int> int main(){ int n; cin>>n; mPair<int, int> * l = new mPair<int, int>[n]; for(int i=0; i<n; ++i) l[i]=makePair(rand()%10,rand()%10); sort(l,l+n,cmp); cout<<l[i].first<<" "<<l[i].second<<endl; }

ШАБЛОНЫ КЛАССОВ

template <typename T> class mStack{ protected: struct element{ T x; element * next; } * head; public: mStack():head(NULL){} void push(T nx){ element * temp = new element; temp->x = nx; temp->next = head; head = temp; } bool empty(){ return !head; void pop(){ if(!empty()){ element * temp = head; head = head->next; delete temp; T top(){ return head->x; };

int main(){ mStack<int> s; int n; cin>>n; for(int i=1; i<=n; ++i) s.push(i); while(!s.empty()){ cout<<s.top()<<" "; s.pop(); }

int main(){ mStack<mPair<int, int> > s; int n; cin>>n; for(int i=1; i<=n; ++i){ s.push(makePair(i,-i)); } while(!s.empty()){ cout<<s.top().first<<" "<<s.top().second<<endl; s.pop();