Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
TemplateRemove
//g++ 7.4.0 //Template remove: remove item from an array of chosen data type //this code is created by Rezaul Hoque on June 09,2021; contact: jewelmrh@yahoo.com //note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; //in C++, template allows a program to use the same function or class for different data types //the function del written in C in previous program is used here in template form to demonstrate one of the strong features of C++ #include <iostream> using namespace std; int n=10; template <class T>//template declaration: keyword template, then typename/class followed by type parameter( a placeholder for other data types) in the angle brackets int retPos(T [], T);//the function template <class T> void del(T [], int (*f)(T [],T )); template <class T> int retPos(T a[],T q){ cout<<"\nItem to be deleted:\n"; cin>>q; for (int i=0; i<n;i++) if(a[i]==q) return i; } template <class T> void del(T a[], int (*f)(T a[],T q)) { int b,x; T q;//item to be deleted of the type chosen by user x=(*f)(a,q); for (b=x;b<n;b++) a[b]=a[b+1]; } int main () { int a[10]= {101,123,124,6,5,90,76,67,45,65}; //an array of integers char c[10]={'k','n','z','y','t','d','s','g','p','q'}; //an array of chars cout<<"\nInitial array:\n"; for(int i=0;i<n;i++) cout<<" "<<a[i]; cout<<"\n"; for(int i=0;i<n;i++) cout<<" "<<c[i]; cout<<"\n"; del(a,retPos); del(c,retPos); cout<<"\nNew array:\n"; for(int i=0;i<n;i++) cout<<" "<<a[i]; cout<<"\n"; for(int i=0;i<n;i++) cout<<" "<<c[i]; cout<<"\n"; return 0; }
run
|
edit
|
history
|
help
0
BridgeEdge
cons1
Stats - Central Limit Theorem - Normal Distribution with multiple items
D.E.Shaw Binary Search Question
Dar
Kadane's Algorithm
Count edges in a graph
mergesort tree
Sort an array of 0s, 1s and 2s
Cours C++