[prog] School problem, C++ and cout.precision(2);

Robert J. Hansen cortana at earthlink.net
Sat Nov 23 19:12:24 EST 2002


As a word of warning: I'm a certified language zealot.  My two preferred
languages are LISP and C++; C++ for when I need fine-grained control
over hardware and LISP for everything else.  I've been a C++ programmer
for thirteen years now.

> How many people use C++ completely ? Actually very few. Is it worth
> learning Templates and other advanced C++ features ?

Absolutely.  No templates = no STL.  No STL = you're not using the best
feature, IMO, of C++.

> I consider Qt/KDE to be a very good C++ library. It uses exactly what is
> required. No more bloat. But still they cant beat the overheads.

The `overhead' of C++ is largely a myth.  There's a hard and fast rule
in the C++ community: You Don't Pay For What You Don't Use.  If you
don't use templates, you don't suffer the runtime penalty for them; if
you don't use vfuncs, you don't suffer the runtime penalty for them;
etc.  YDPFWYDU--which is pretty much C++'s answer to Perl's TMTOWTDI--is
sometimes ignored by compiler authors.  For instance, Sun's compiler was
for years infamously bad with templates; the most trivial use of
templates could bloat code size by over a meg.

On the other hand, though, GCC 3.2 is acceptably good.  The /linker/ is
still horrible, but the compiler is pretty good.  Under glibc, C++
programs often have unacceptable startup times because of some pretty
poor linker behavior.  The GCC crew is trying to address this issue.

> I believe C++ comes with its own classes for implementing stacks, hashes
> and other data structures. Does anybody use them ?

All the time.  Using them has probably doubled my productivity and
halved my bugs.  No, I'm not kidding.

> I personally do not like streams. Its more trouble than ease. And, I
> love strongly typed languages (you can call PHP an exception ;-))

C++ is a strongly typed language, which is why in the archives I was
surprised to see you using C-style (typeless) casts.  C++ provides
typesafe casts.

As a short example of some C++ which makes full use of templates and the
STL...

=====

#include <iostream>
#include <iterator>
#include <functional>
#include <algorithm>
#include <vector>

using namespace std;

typedef unsigned long u32;
typedef vector<u32>::iterator vu32iter;

void Sieve(const u32 candidate, vector<u32>* primes)
{
  vu32iter idx = primes->begin();
  while (idx != primes->end())
    if (candidate % *idx++ == 0) return;
  primes->push_back(candidate);
}

int main(void)
{
  vector<u32> numbers, primes;
  for (u32 x = 2 ; x < 100000 ; x++) numbers.push_back(x);
  for_each(numbers.begin(), numbers.end(), 
    bind2nd(ptr_fun(Sieve), &primes));
  copy(primes.begin(), primes.end(), ostream_iterator<u32>(cout, " "));
  cout << endl;
  return 0;
}

=====

The Sieve of Eratosthenes in twenty-nine lines, the first twelve of
which are just spent with language semantics.  It makes use of
templates, the STL and functional programming, and weighs in at a slim
11k of compiled code.

Finding the first 100,000 primes takes about eight seconds on my
P3-800.  I could probably find ways to optimize this for speed, but for
right now I just want to show that C++ isn't bloated and isn't slow,
even when using what are usually thought of as the more bloated and slow
language features.  :)

=====

As another example, Codebook--a GnuPG GUI that I'm working on--uses
templates, objects and virtual function calls extensively.  All three of
them are usually considered to be bloatware.  But Codebook manages to
come in at under 180k, which isn't bad at all for an app.





More information about the Programming mailing list