STL, is it worth it?

Programming, for all ages and all languages.
Post Reply
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

STL, is it worth it?

Post 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?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post 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.
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post 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?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post 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);
      }
   }
};
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post by eboyd »

OK, but my question is, what's the point of doing it that way when it's uneccesary?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post 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.
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post 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.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post 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.
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Common....

Post 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?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: Common....

Post 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
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Post 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
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
Post Reply