Simple Function Template Array Program Example : Search Number
Generic Programming
Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types
Function Template Definition
A function template behaves same as normal function except that the template works with different data types.
Function Template Syntax
template <class identifier> function_declaration;
template <typename identifier> function_declaration;
templete_identifier fn_name(templete_identifier .. args) {
return ...
}
Example Program
// Header Files
#include <iostream>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 5
using namespace std;
// Template Declaration
template<class T>
// Generic Template Function for Search
T getSearch(T x[], T element) {
int i;
cout << "\nYour Data :";
for (i = 0; i < MAX_SIZE; i++) {
cout << "\t" << x[i];
}
/* for : Check elements one by one - Linear */
for (i = 0; i < MAX_SIZE; i++) {
/* If for Check element found or not */
if (x[i] == element) {
cout << "\nFunction Template : Element : " << element << " : Found : Position : " << i + 1 << ".\n";
break;
}
}
if (i == MAX_SIZE)
cout << "\nSearch Element : " << element << " : Not Found \n";
}
int main() {
int arr_search[MAX_SIZE], i, element;
float f_arr_search[MAX_SIZE], f_element;
cout << "Simple Function Template Array Program Example : Search Number\n";
cout << "\nEnter " << MAX_SIZE << " Elements for Searching Int : " << endl;
for (i = 0; i < MAX_SIZE; i++)
cin >> arr_search[i];
cout << "\nEnter Element to Search (Int) : ";
cin>>element;
// Passing int Array to Template Function
getSearch(arr_search, element);
cout << "\nEnter " << MAX_SIZE << " Elements for Searching Float : " << endl;
for (i = 0; i < MAX_SIZE; i++)
cin >> f_arr_search[i];
cout << "\nEnter Element to Search (Float) : ";
cin>>f_element;
// Passing int Array to Template Function
getSearch(f_arr_search, f_element);
getch();
return 0;
}
Sample Output
Simple Function Template Array Program Example : Search Number
Enter 5 Elements for Searching Int :
56
34
100
23
90
Enter Element to Search (Int) : 23
Your Data : 56 34 100 23 90
Function Template Search : Element : 23 : Found : Position : 4.
Enter 5 Elements for Searching Float :
45.23
89.01
90.67
100.89
23.90
Enter Element to Search (Float) : 23.90
Your Data : 45.23 89.01 90.67 100.89 23.9
Function Template Search : Element : 23.9 : Found : Position : 5.
Templates In C++
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Example Program For Copy Constructor In C++
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Example Program For Constructor Overloading In C++
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Do While Loop Example Program In C++
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Exception Handling Divide by zero Using C++ Programming