Usefull C++ Tricks


How To Read Unknown Numbers Of Input?

while(std::cin >> value)
{
    ...
}

How To define a reference to an array of ten pointers?

int *(&arry)[10] = ptrs;

How To reset an object in a better way?

void reset(int &i)
{
    i = 0;
}
int j = 10;
reset(j);

Class Templete

#include <iostream>
using namespace std;

template <class T>
class mypair {
    T a, b;
  public:
    mypair (T first, T second)
      {a=first; b=second;}
    T getmax ();
};

template <class T>
T mypair<T>::getmax ()
{
  T retval;
  retval = a>b? a : b;
  return retval;
}

int main () {
  mypair <int> myobject (100, 75);
  cout << myobject.getmax();
  return 0;
}

Welcome to share or comment on this post:

Table of Contents