=================DR. DOBB'S JOURNAL===============
ARTICLE:       C++ 
ISSUE:         August 2006
SECTION:       Column Well
AUTHOR:        Pete Becker
               % Roundhouse Consulting, Ltd.
               2114 S. Smith Rd.
               Bloomington, IN 47401
               pbecker@acm.org.
The New C++
by Pete Becker

=================================

for (int i = 0; i < vect.size(); ++i)
  // do whatever you need to do with i

=================================

const int limit = vect.size();
for (int i = 0; i < limit; ++i)
  // do whatever you need to do with i


=================================

{
const int limit = vect.size();
for (int i = 0; i < limit; ++i)
  // do whatever you need to do with i
}


=================================

char buffer[BUF_SIZE];
void expensive_function()
{
// code using buffer
}

void cheaper_version()
{
static char *buffer = new char[BUF_SIZE];
// code using buffer
}


=================================

double really_slow_thing(double);
double cached_version(double arg)
{
static double cached_argument;
static double cached_result;
static bool cache_valid = false;
if (!cache_valid || cached_argument != arg)
  {
  cache_valid = true;
  cached_argument = arg;
  cached_result = really_slow_thing(arg);
  }
return cached_result;
}

=================================

double r0 = really_slow_thing(0.0);
double r1 = really_slow_thing(1.0);
double r2 = really_slow_thing(2.0);
double precomputed_version(double arg)
{
return arg == 0.0 ? r0
  : arg == 1.0 ? r1
  : arg == 2.0 ? r2
  : really_slow_thing(arg);
}
1


