Search
 
SCRIPT & CODE EXAMPLE
 

CPP

C++ template example

#include <iostream>
using namespace std;
  
// One function works for all data types.  This would work
// even for user defined types if operator '>' is overloaded
template <typename T> T myMax(T x, T y)
{
    return (x > y) ? x : y;
}
  
int main()
{
    cout << myMax<int>(3, 7) << endl; // Call myMax for int
    cout << myMax<double>(3.0, 7.0)
         << endl; // call myMax for double
    cout << myMax<char>('g', 'e')
         << endl; // call myMax for char
  
    return 0;
}
Comment

c++ template function

template <class T>
void swap(T & lhs, T & rhs)
{
 T tmp = lhs;
 lhs = rhs;
 rhs = tmp;
}
Comment

template c++

#include <iostream>
using namespace std;
 
// One function works for all data types.  This would work
// even for user defined types if operator '>' is overloaded

template <typename T>
T myMax(T x, T y)
{
   return (x > y)? x: y;
}
 
int main()
{
  cout << myMax<int>(3, 7) << endl;  // Call myMax for int
  cout << myMax<double>(3.0, 7.0) << endl; // call myMax for double
  cout << myMax<char>('g', 'e') << endl;   // call myMax for char
 
  return 0;
}
Comment

what is a template in c++

A template is a simple and yet very powerful tool in C++. The simple idea is to
pass data type as a parameter so that we don’t need to write the same code for
different data types.

For example, a software company may need sort() for
different data types. Rather than writing and maintaining the multiple codes,
we can write one sort() and pass data type as a parameter.
  
C++ adds two new keywords to support templates: ‘template’ and ‘typename’. The
second keyword can always be replaced by keyword ‘class’.
Comment

c++ template

template <class T>
class mypair {
    T values [2];
  public:
    mypair (T first, T second)
    {
      values[0]=first; values[1]=second;
    }
};
Comment

C++ template function

/*
C++ template functions are an alternamive way to write a function that
can take different data types. C++ Template functions are only one
function, so if you need to make a change, then it only has to be done
once. Here is an example of a 'get_doubled' templated function:
*/

#include <iostream>
using std::cout;

template <typename T> // Now, T is a type of variable, for this scope:
void say_something(T input) {
  cout << input << "
";
}

int main(void) {
  say_something(45); // Uses a int
  say_something("Hello"); // Uses a string
  say_something(90.5); // Uses a float/double
  return 0;
}
Comment

template c++

template <class identifier> function_declaration;
template <typename identifier> function_declaration;

//Example:
template <class Type> 
void Swap( Type &x, Type &y)
{
    Type Temp = x;
    x = y; 
    y = Temp;
}
Comment

template c++

template <class T>
void foo(T a, T b) {
 ...
}
Comment

function template c++

template<class T1 , class T2>
float fun(T1 a, T2 b){
    float avg2 = (a+b)/2.0;
    return avg2;
}
Comment

c++ template

int x,y;
GetMax <int> (x,y);
Comment

template in cpp

// templates are used if the logic of function is same but 
// it differs due to data type.

/*Here is an example to show how user defined function print 
can be made to print  data of different data types.*/ 

template <class T>
T print (T a)
{
   cout << a;
}

int main()
{
    print('a');
    print(1);
}
Comment

C++ Templates as function

template <class T>
class Number {
    ... .. ...
    // Function prototype
    T getnum();
};

// Function definition
template <class T>
T Number<T>::getNum() {
    return num;
}
Comment

templates of templates c++

namespace std {
  template<typename t> struct hash<MyClass<t>>
  {
  	size_t operator() (const MyClass<t>& c) const;
  }
}

// You can also do things like

template<template<typename t> class type> func_name<type<t>>();
Comment

Function Template in c++

// function template in c++
#include <iostream>
using namespace std;
float funAvg(int a, int b){
    float avg = (a+b)/2.0;
    return avg;
}
float funAvg2(int a, float b){
    float avg2 = (a+b)/2.0;
    return avg2;
}

//function template in c++

template<class T1 , class T2>
float fun(T1 a, T2 b){
    float avg2 = (a+b)/2.0;
    return avg2;
}

int main(){
 
    float a;
    a = funAvg(22 , 7);
    printf("The average of these number is %.3f ",a);
    cout<<endl;
    float a1;
    a1 = funAvg2(11 , 8.6);
    printf("The average of these number is %.3f ",a1);
    cout<<endl;
    // float T;
    // T = fun(11 , 8.6f);
    // printf("The average of these number is %.3f ",T);
    // cout<<endl;
    // ---------------------function template in c++-----------------
    float T;
    T = fun(11 , 98);
    printf("The average of these number is %.3f ",T);


    return 0;
}
Comment

c++ template

template <class  T>//or <typename T> it´s the same
 //T can be int, float, double, etc. 
//simple use example: 
T sum(T a, T b)
{
  return a + b;
}
sum(5.0f, 10f);//sum using float
sum(2,3);//sum using int
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ vector move element to front 
Cpp :: c++ clear char array 
Cpp :: check if character in string c++ 
Cpp :: divide and conquer based algorithm to find maximum and minimum of an array 
Cpp :: 1523. Count Odd Numbers in an Interval Range solution in c++ 
Cpp :: built in factorial function in c++ 
Cpp :: c++ remove last character from string 
Cpp :: string substr c++ 
Cpp :: delete dynamic array c++ 
Cpp :: c++ triple 
Cpp :: c++ pause linux 
Cpp :: why is using namespace std a bad practice 
Cpp :: C++ Volume of a Cylinder 
Cpp :: c++ encapsulation 
Cpp :: team fortress 
Cpp :: how to read files in c++ 
Cpp :: how to print a text in c++ 
Cpp :: std vector random shuffle 
Cpp :: how to add c++14 in sublime text 
Cpp :: cpp float 
Cpp :: convert characters to lowercase c++ 
Cpp :: copying a set to vector in c++ 
Cpp :: C++ Conditions and If Statements 
Cpp :: length of number c++ 
Cpp :: linked list in c++ 
Cpp :: c++ range based for loop 
Cpp :: rotate an array of n elements to the right by k steps 
Cpp :: system cpp 
Cpp :: c++ inheritance constructor 
Cpp :: how to check char array equality in C++ 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =