TECH: STL / Heaps, etc. (was: [MUD-Dev] TECH DGN: a few mud server design questions (long))

Caliban Tiresias Darklock caliban at darklock.com
Mon Aug 6 20:55:46 CEST 2001


On Sat, 4 Aug 2001 10:19:38 +0100, "Adam Martin" <ya_hoo_com at yahoo.com>
wrote:

> surely the point is that with all templates and standard libraries
> there is a high barrier to using them the first time, but once you
> are familiar with them (i.e. have used the various bits a few
> times) you can make new programs much faster (to code, and one
> would hope usually to run too, in the case of libraries) than you
> could using your own hacks, otherwise what is the point?

To solve hard problems. If your problem is not hard, the library may
be overkill. A stack of 100 integers, for example, could be
implemented with STL:

  #include<stack>
  using namespace std;
  stack<int> s;

Or with straight C++ types:

  int a[100];
  int i=-1;

Let's look at how these work.

Push something on the stack.

  s.push(x);
  a[++i]=x;

Pop something off the stack.

  s.pop();
  --i;

Is the stack empty?

  if(s.empty()) ;
  if(i==-1) ;

What's on the top of the stack?

  s.top();
  a[i];

How much stuff is on the stack?

  s.size();
  i+1;

The STL version has some great features, *if* you have a harder
problem.  If your stack may contain thousands of objects, or you
need a lot of stacks, or the object on the stack is more complex
than a simple int, STL will make things a lot easier. A stack of
parse trees, for example, would almost certainly be better
implemented with STL. But for this particular problem, STL is just
plain too much -- and the straight C++ version is a great deal
smaller and faster.

_______________________________________________
MUD-Dev mailing list
MUD-Dev at kanga.nu
https://www.kanga.nu/lists/listinfo/mud-dev



More information about the mud-dev-archive mailing list