Page 1 of 1

STL, is it worth it?

Posted: Fri Aug 03, 2007 8:10 am
by eboyd
I've been using C++ for years now and one thing I could never bring myself
to do is use the Standard Template Library.

I've always believed in the convention that your ADTs should be problem specific. Meaning: I don't see the use of something like the List container because any linkable structures I would make would have very different operations from that defined in the STL.

How do the rest of you feel?

Posted: Fri Aug 03, 2007 8:32 am
by Candy
Get used to the STL, learn how it's made, why stuff is like it is and then think of whether you can make a better one. If you can, for all things, please do. If you cannot, use the STL.

Hint: most people don't recreate something.

Posted: Fri Aug 03, 2007 10:16 am
by eboyd
For example:

The last project I worked on, I had to create a file-system structure. It was basically a multi-list structure that had to support multiple different file extensions in different ways. That said, for the whole thing to work we had to use a lot of explicit type-casting for operations on the different extensibles(objects when it comes down to it).

Code: Select all

system_object = (whatever_extension*)generic_file;
Admittedly I don't know if the above will work under an implementation of the STL.

Furthermore, I don't understand how I could add other pointers to the doubly-linked list supplied by the STL. For my directory structure to work, I needed at least two pointers, one to the next in line and another to a head object which is actually the first object in the list for the given directory.

Code: Select all

class base
{
    protected:
    base* next;
    base* pdir;  // points to the previous directory of the given object

    etc. etc.
}

class directory : public base
{
    private:
    base* dir_head; // points to the first item within the directory
    
    etc. etc.
}

    
I guess my point is I haven't had a need to use a simple list with different data type capabilities, instead I've needed to add different functionality to my objects/adts.

Admittedly, I don't know much about the STL, because I've never used it. So how would I benefit in this example or similar ones by using it?

Posted: Fri Aug 03, 2007 11:09 am
by Candy

Code: Select all

class file {};

class directory : public file {
   std::list<file *> files;
   void forall(void (*func)(file *file)) {
      std::list<file *>::iterator b = files.begin();
      for (; b != files.end(); ++b) {
         func(*b);
         if ((directory *dir = dynamic_cast<directory *>(*b)) != 0)
            dir->forall(func);
      }
   }
};

Posted: Fri Aug 03, 2007 11:53 am
by eboyd
OK, but my question is, what's the point of doing it that way when it's uneccesary?

Posted: Fri Aug 03, 2007 12:29 pm
by Candy
eboyd wrote:OK, but my question is, what's the point of doing it that way when it's uneccesary?
What's unnecessary about it?

Why do you make functions? To remove repeated code so you don't make oversight-mistakes in repeating it. Why would you use a standard list? Because everybody understands it (lots of people at least) and because it's probably faster than any other method. In any case, it reduces the implementation count to one and allows the implementation to be optimized.

Get used to using these things for simple tasks and you can encapsulate very complex things in these containers. Not to mention make generic algoritms to be used on them (such as in the algorithm header).


My main reason: imagine 500 thousand lines of code written with all the back pointer and forward pointer management in there at least a thousand times over, each time slightly different. Now, one of these has a bug. Good luck.

Posted: Fri Aug 03, 2007 12:37 pm
by eboyd
So if you design a class, how do you *add* different functions and/or fields to an existing template class?

I should add that I'm only a student of programming right now and I realize I have much to learn. The fact that you mentioned *optimized* makes me a little more at ease, as in my own personal learning I always want to know the most efficient ways of doing things. I personally feel that no matter how many terabytes of RAM will be available in the future, there's no reason to be sloppy now or then.

Posted: Fri Aug 03, 2007 1:25 pm
by Candy
eboyd wrote:So if you design a class, how do you *add* different functions and/or fields to an existing template class?
You normally don't add fields to a template class, especially STL classes. You implement and design your classes using them as building blocks. Read up on the Liskov substitution principle (LSP) to find out why you should not inherit from a list if you use a list to implement it. A directory is not a generic list, a directory is implemented in terms of a generic list specialized for files.
The fact that you mentioned *optimized* makes me a little more at ease, as in my own personal learning I always want to know the most efficient ways of doing things.
Premature optimization is the root of all evil (Donald Knuth). Don't make designs needlessly unoptimized but don't make them needlessly complicated just to get the most efficient routine out of it. First and foremost aim for readable and maintainable code, then design for sufficiently efficient code, then do whatever you like to do.

[quiote]I personally feel that no matter how many terabytes of RAM will be available in the future, there's no reason to be sloppy now or then.[/quote]

That's very true. I can attest to that from a number of real life programs who do not consider cpu cycles important.

Common....

Posted: Sat Aug 18, 2007 9:48 am
by DeletedAccount
Hi guys,
STL is a really powerfull library and it does many things for you....
STL is an integral part of most of the compiler suites ... STL
off course is useful and speeds up development time atleast
by 30% -- 70% --- But when you are learning data structures
implement them by yourselves ... otherwise simply use the
STL ... STL indeed is good ... when some has done a good work
one should appricicate them and make use of it .......

At this point the ideal question should be when to use STL
and when not?

Re: Common....

Posted: Sat Aug 18, 2007 11:47 am
by Candy
SandeepMathew wrote: At this point the ideal question should be when to use STL
and when not?
Use it when you can and when it's optimal. If you know of a more optimal solution and you need the increase or if it just doesn't give a good match, don't use it.

That means, don't use it on very small processors (8-bit, low memory, that kind of stuff), don't use unbounded types in hard realtime applications

Posted: Sat Aug 18, 2007 12:15 pm
by Colonel Kernel
Also don't use it if your code has to be portable to ridiculously old versions of UNIX with C++ compilers from 1996. :P