C++/Function/generic parameters

Материал из C\C++ эксперт
Перейти к: навигация, поиск

A Function with Two Generic Types

   
#include <iostream>
using namespace std;
   
template <class type1, class type2>
void myfunc(type1 x, type2 y)
{
  cout << x << " " << y << "\n";
}
   
int main()
{
  myfunc(10, "I like C++");
   
  myfunc(98.6, 19L);
   
  return 0;
}


Applying Generic Functions: A Generic Bubble Sort

   
#include <iostream>
using namespace std;
   
template <class X> void bubble(
  X *items,  // pointer to array to be sorted
  int count) // number of items in array
{
  register int a, b;
  X t;
   
  for(a=1; a<count; a++)
    for(b=count-1; b>=a; b--)
      if(items[b-1] > items[b]) {
        // exchange elements
        t = items[b-1];
        items[b-1] = items[b];
        items[b] = t;
      }
}
   
int main()
{
  int iarray[7] = {7, 5, 4, 3, 9, 8, 6};
  double darray[5] = {4.3, 2.5, -0.9, 100.2, 3.0};
   
  for(int i=0;  i<7; i++)
    cout << iarray[i] << endl;
   
  for(int i=0;  i<5; i++)
    cout << darray[i] << endl;
   
  bubble(iarray, 7);
  bubble(darray, 5);
   
  for(int i=0;  i<7; i++)
    cout << iarray[i] << endl;
   
  for(int i=0;  i<5; i++)
    cout << darray[i] << endl;
   
  return 0;
}


Cast generic parameters

  
#include <iostream>
using namespace std;
template <class T1, class T2>
bool coerce(T1& x, T2 y)
{
   if (sizeof(x) < sizeof(y))
      return false;
   x = static_cast<T1>(y);
   return true;
}
int main()
{
   int i, j;
   float  x;
   double y;
   char   ch1;

   i = "a";   ch1 = "b";
   cout << "\ncoerce int, ch  = ";
   cout << coerce(i, ch1);
   cout << "   i = " << i << "  ch1 = " << ch1;
   i = "a";  ch1 = "b";
   cout << "\ncoerce ch,  int = ";
   cout << coerce(ch1, i);
   cout << "   i = " << i << "  ch1 = " << ch1;
   x = 1.1;   y = 2.2;
   cout << "\ncoerce fl,  dbl = ";
   cout << coerce(x, y);
   cout << "   x = " << x << "  y = " << y;
   x = 1.1;  y = 2.2;
   cout << "\ncoerce dbl, fl  = ";
   cout << coerce(y, x);
   cout << "   x = " << x << "  y = " << y;
   cout << endl;   cin>> i;
}


Generic Function Restrictions

   
#include <iostream>
#include <cmath>
using namespace std;
   
void myfunc(int i)
{
  cout << "value is: " << i << "\n";
}
   
void myfunc(double d)
{
  double intpart;
  double fracpart;
   
  fracpart = modf(d, &intpart);
  cout << "Fractional part: " << fracpart;
  cout << "\n";
  cout << "Integer part: " << intpart;
}
   
int main()
{
  myfunc(1);
  myfunc(12.2);
   
  return 0;
}


Generic function template

   
#include <iostream>
using namespace std;
   
template <class X> void swapargs(X &a, X &b){
  X temp;
   
  temp = a;
  a = b;
  b = temp;
}
   
int main(){
  int i=10, j=20;
  double x=10.1, y=23.3;
  char a="x", b="z";
   
  cout << "Original i, j: " << i << " " << j << "\n";
  cout << "Original x, y: " << x << " " << y << "\n";
  cout << "Original a, b: " << a << " " << b << "\n";
   
  swapargs(i, j); // swap integers
  swapargs(x, y); // swap floats
  swapargs(a, b); // swap chars
   
  cout << "Swapped i, j: " << i << " " << j << "\n";
  cout << "Swapped x, y: " << x << " " << y << "\n";
  cout << "Swapped a, b: " << a << " " << b << "\n";
   
  return 0;
}


template for value output

  
#include <iostream>
using namespace std;
template <class T1, class T2> void sample(T1 x, T2 y);
int main(void)
{
   sample(10, "hi");
   sample(0.23, 10L);
   sample("Jamsa"s", "C/C++");
}
template <class T1, class T2> void sample(T1 x, T2 y)
{
   cout << x << " " << y << endl;
}