To STL or not to?

S

Stephan Rose

Hey everyone,

I was just wondering what the general consensus is on STL these days.

I think that if I remember right, several years ago, most people (if not
everyone) I knew anyway stayed far away from it.

I then switched to C# for several years and didn't worry about it much,
but having switched back to more C++ work earlier this year, I was just
wondering about that. =)

One of my projects does make heavy use of various arrays where I am
currently using custom made classes for that purpose and here I was just
pondering if potentially STL may be a better idea.

Any thoughts / suggestions would be appreciated,

Thanks,

--
Stephan
2003 Yamaha R6

å›ã®ã“ã¨æ€ã„出ã™æ—¥ãªã‚“ã¦ãªã„ã®ã¯
å›ã®ã“ã¨å¿˜ã‚ŒãŸã¨ããŒãªã„ã‹ã‚‰
 
A

Alf P. Steinbach

* Stephan Rose:
Hey everyone,

I was just wondering what the general consensus is on STL these days.

I think that if I remember right, several years ago, most people (if not
everyone) I knew anyway stayed far away from it.

I then switched to C# for several years and didn't worry about it much,
but having switched back to more C++ work earlier this year, I was just
wondering about that. =)

One of my projects does make heavy use of various arrays where I am
currently using custom made classes for that purpose and here I was just
pondering if potentially STL may be a better idea.

Yes.

And Boost.


Cheers, & hth.,

- Alf
 
J

Jim Langston

Stephan Rose said:
Hey everyone,

I was just wondering what the general consensus is on STL these days.

I think that if I remember right, several years ago, most people (if not
everyone) I knew anyway stayed far away from it.

I then switched to C# for several years and didn't worry about it much,
but having switched back to more C++ work earlier this year, I was just
wondering about that. =)

One of my projects does make heavy use of various arrays where I am
currently using custom made classes for that purpose and here I was just
pondering if potentially STL may be a better idea.

Any thoughts / suggestions would be appreciated,

I make extensive use of the standard containers of all types (vector, set,
map, etc...). There seems to be little penalty for using them as long as
you try to keep in your head that it can be expensive to copy an entire
container, passing by reference is prefered.

As far as this group goes, I would think the general consensus would be to
use the STL.
 
R

Roland Pibinger

One of my projects does make heavy use of various arrays where I am
currently using custom made classes for that purpose and here I was just
pondering if potentially STL may be a better idea.

But isn't std::vector just a thin wrapper over realloc?
 
A

Andreas E. Mueller

One of my projects does make heavy use of various arrays where I am
currently using custom made classes for that purpose and here I was just
pondering if potentially STL may be a better idea.

Hello Stephan,

well, it sound's like asking myself "should I invent the wheel again?" for
me.

I prefer using the STL in case I have to use complex array types. The only
exception is when I need something to run more than optimal or
minimalistic. Then mostly try to implement my own arrays.

For example I had to use a kind of key string indexed array for a project,
but it had to be fast by searching at the same time. So I had to implement
my own array type. But these are really rare situations for me.

In the most cases, if I have to use something already invented, which
would do the work for me, for example a vector, then I would not crash my
head against the wall to implement a new vector class. This would be
definitely a waste of precious time and braincells.

As you can see, it's all a matter of the given tasks you write your
program for.

Hope this can help you to decide... This is only my way about using the
STL or not. It doesn't mean that you have to follow this way too. *wink*

Greets,
Andreas E. Mueller

PS. Sorry about my English, I'm not a native English speaker, but I'm
still trying to get better on it.
 
W

werasm

One of my projects does make heavy use of various arrays where I am
currently using custom made classes for that purpose and here I was just
pondering if potentially STL may be a better idea.

Don't ponder. Use it! Especially the container classes has made life
allot
easier - vector replacing arrays the most notable for me. Also,
algorithms
for sorting, as well as sets and maps that would be quite a feat to
implement
yourself. In conjunction with boost::bind algorithms become easier to
implement
as well. I read Nicolai Josuttis's "The C++ Standard Library" prior to
using
it, and I thought it got me up to speed quite quickly - one of the
easiest
reads (IMHO).

Regards,

Werner
 
S

Stephan Rose

Don't ponder. Use it! Especially the container classes has made life
allot
easier - vector replacing arrays the most notable for me. Also,
algorithms
for sorting, as well as sets and maps that would be quite a feat to
implement
yourself. In conjunction with boost::bind algorithms become easier to
implement
as well. I read Nicolai Josuttis's "The C++ Standard Library" prior to
using
it, and I thought it got me up to speed quite quickly - one of the
easiest
reads (IMHO).

Thanks very much for your input everyone, very much appreciated.

STL it shall be then. =)

--
Stephan
2003 Yamaha R6

å›ã®ã“ã¨æ€ã„出ã™æ—¥ãªã‚“ã¦ãªã„ã®ã¯
å›ã®ã“ã¨å¿˜ã‚ŒãŸã¨ããŒãªã„ã‹ã‚‰
 
S

Stephan Rose

Hello Stephan,

well, it sound's like asking myself "should I invent the wheel again?"
for me.

I prefer using the STL in case I have to use complex array types. The
only exception is when I need something to run more than optimal or
minimalistic. Then mostly try to implement my own arrays.

For example I had to use a kind of key string indexed array for a
project, but it had to be fast by searching at the same time. So I had
to implement my own array type. But these are really rare situations for
me.

In the most cases, if I have to use something already invented, which
would do the work for me, for example a vector, then I would not crash
my head against the wall to implement a new vector class. This would be
definitely a waste of precious time and braincells.

As you can see, it's all a matter of the given tasks you write your
program for.

Hope this can help you to decide... This is only my way about using the
STL or not. It doesn't mean that you have to follow this way too. *wink*

Greets,
Andreas E. Mueller

PS. Sorry about my English, I'm not a native English speaker, but I'm
still trying to get better on it.

Your English is quite good actually. To be honest, had you not mentioned
it I don't think I would have known.

My second language for me actually, working on my 3rd now. =)

Thanks for your input btw. =)


--
Stephan
2003 Yamaha R6

å›ã®ã“ã¨æ€ã„出ã™æ—¥ãªã‚“ã¦ãªã„ã®ã¯
å›ã®ã“ã¨å¿˜ã‚ŒãŸã¨ããŒãªã„ã‹ã‚‰
 
D

Daniel T.

Stephan Rose said:
I was just wondering what the general consensus is on STL these days.

Use it if you can. I am in the unfortunate position that my platform
doesn't support it and/or my bosses don't allow it. (They said the
platform doesn't support it and I have never pushed the issue.)
 
J

James Kanze

I was just wondering what the general consensus is on STL these days.

It's the standard. You "assume" that every C++ programmer is
more or less familiar with it, in the same way that he is
familiar with other parts of the language.
I think that if I remember right, several years ago, most
people (if not everyone) I knew anyway stayed far away from
it.

A quality implementation requires a compiler which is close to
conformant. Several years ago, there were still a lot of
compilers missing major features, with the result that STL
implementations varied in their work arounds, and weren't
necessarily compatible. That's not the case today. (Although I
still occasionally have problems with Sun CC; the compiler has
finally become "pretty good", but both of the library
implementations delivered with it are "pretty bad"---partially,
I think, because they haven't been updated to reflect the
capabilities of the new compiler, but only partially.)
I then switched to C# for several years and didn't worry about it much,
but having switched back to more C++ work earlier this year, I was just
wondering about that. =)
One of my projects does make heavy use of various arrays where I am
currently using custom made classes for that purpose and here I was just
pondering if potentially STL may be a better idea.

The STL has one absolute advantage: it is standard. Anyone
coming into your project from elsewhere should be more or less
familiar with the interface to std::vector, etc. Where as he
will first have to learn your in house classes. The STL may not
be an example of good design, but it's good enough, and it's
standard, and that means that it should be used by default.
 
T

tony_in_da_uk

But isn't std::vector just a thin wrapper over realloc?

I wish it always was. Enough of all this inefficient new / copy /
delete crap even when it's not needed....

Tony
 
P

Puppet_Sock

The STL has one absolute advantage: it is standard. Anyone
coming into your project from elsewhere should be more or less
familiar with the interface to std::vector, etc. Where as he
will first have to learn your in house classes. The STL may not
be an example of good design, but it's good enough, and it's
standard, and that means that it should be used by default.

The design of the STL is pretty good for what it is
intended for. That is, it is intended to be a fairly
general use thing, reasonably efficient for many
tasks. If you have a very special task it is *possible*
that you may need to "roll your own" to obtain some
specific improvement. Say, something related to some
particular hardware or some such. However, it is
quite likely that it would take a huge amount of
effort, and require a much better than average coder,
to get a better design than the STL. A lot of really
smart people worked pretty hard on it.

It's worth expanding on the advantages of a standard.

The existing STL containers and algorithms are already
extensively documented. This means that a project that
makes use of them does not have to create docs for that
part of the code. If you "roll your own" you will have
to not only do all the design and development effort,
but you will have to write documentation for it, test
suites, regression testing, test reports, etc. For
projects where QA is a big deal, this could be a big
amount of work saved.

A reasonably good STL implementation will be unlikely
to have any bugs. So, when you test your code you will
be confident that any problems are not in the STL parts.
With a "roll your own" you are going to be tempted to
go on a bug hunt through the container code.

If you do find a bug in the container code, it may have
a cascade effect through the entire project. Usually
the cost of fixing any given bug will increase quite
rapidly over time, particularly when that code is
used by other code over large parts of the project.
Thus having a standard library for widely used
things like <vector> is a Very Good Thing.

If you do find a bug in one implementation of the STL,
it is possible to get another implementation, often
from a completely different vendor. The interface
is the same and so you should simply be able to
re-compile with the new implementation and continue
on as before.

There is a very wide community of people who have
extensively used the STL. Thus you can ask questions
about proper use, efficient use, problems understanding,
compiler issues, etc., and have a large number of
people who may know the answer. With a "roll your
own" the number of users is probably much smaller.
A problem could delay you till you solve it yourself.

If you work on an STL project, your experience will
count when people are hiring for other STL projects.
Your experience on a "roll your own" probably won't.
This will quite likely mean you get a higher rate
on the next project. Or can pick and choose what
projects you want instead of taking whatever you
can get.
Socks
 
J

James Kanze

I was just wondering what the general consensus is on STL these days.

It's the standard. You "assume" that every C++ programmer is
more or less familiar with it, in the same way that he is
familiar with other parts of the language.
I think that if I remember right, several years ago, most
people (if not everyone) I knew anyway stayed far away from
it.

A quality implementation requires a compiler which is close to
conformant. Several years ago, there were still a lot of
compilers missing major features, with the result that STL
implementations varied in their work arounds, and weren't
necessarily compatible. That's not the case today. (Although I
still occasionally have problems with Sun CC; the compiler has
finally become "pretty good", but both of the library
implementations delivered with it are "pretty bad"---partially,
I think, because they haven't been updated to reflect the
capabilities of the new compiler, but only partially.)
I then switched to C# for several years and didn't worry about it much,
but having switched back to more C++ work earlier this year, I was just
wondering about that. =)
One of my projects does make heavy use of various arrays where I am
currently using custom made classes for that purpose and here I was just
pondering if potentially STL may be a better idea.

The STL has one absolute advantage: it is standard. Anyone
coming into your project from elsewhere should be more or less
familiar with the interface to std::vector, etc. Where as he
will first have to learn your in house classes. The STL may not
be an example of good design, but it's good enough, and it's
standard, and that means that it should be used by default.
 
S

Stephan Rose

The design of the STL is pretty good for what it is intended for. That
is, it is intended to be a fairly general use thing, reasonably
efficient for many tasks. If you have a very special task it is
*possible* that you may need to "roll your own" to obtain some specific
improvement. Say, something related to some particular hardware or some
such. However, it is quite likely that it would take a huge amount of
effort, and require a much better than average coder, to get a better
design than the STL. A lot of really smart people worked pretty hard on
it.

It's worth expanding on the advantages of a standard.

The existing STL containers and algorithms are already extensively
documented. This means that a project that makes use of them does not
have to create docs for that part of the code. If you "roll your own"
you will have to not only do all the design and development effort, but
you will have to write documentation for it, test suites, regression
testing, test reports, etc. For projects where QA is a big deal, this
could be a big amount of work saved.

A reasonably good STL implementation will be unlikely to have any bugs.
So, when you test your code you will be confident that any problems are
not in the STL parts. With a "roll your own" you are going to be tempted
to go on a bug hunt through the container code.

If you do find a bug in the container code, it may have a cascade effect
through the entire project. Usually the cost of fixing any given bug
will increase quite rapidly over time, particularly when that code is
used by other code over large parts of the project. Thus having a
standard library for widely used things like <vector> is a Very Good
Thing.

If you do find a bug in one implementation of the STL, it is possible to
get another implementation, often from a completely different vendor.
The interface is the same and so you should simply be able to re-compile
with the new implementation and continue on as before.

There is a very wide community of people who have extensively used the
STL. Thus you can ask questions about proper use, efficient use,
problems understanding, compiler issues, etc., and have a large number
of people who may know the answer. With a "roll your own" the number of
users is probably much smaller. A problem could delay you till you solve
it yourself.

If you work on an STL project, your experience will count when people
are hiring for other STL projects. Your experience on a "roll your own"
probably won't. This will quite likely mean you get a higher rate on the
next project. Or can pick and choose what projects you want instead of
taking whatever you can get.
Socks

Thanks again very much for the additional responses, very much
appreciated.

I ended up switching quite a lot of things to STL containers and things
are working very well, I'm very happy with it. Even though the API I use,
wxWidgets, does provide quite a few ready made container classes I'm
finding the STL stuff to be nicer to use. I'm very happy about making
this decision.

--
Stephan
2003 Yamaha R6

å›ã®ã“ã¨æ€ã„出ã™æ—¥ãªã‚“ã¦ãªã„ã®ã¯
å›ã®ã“ã¨å¿˜ã‚ŒãŸã¨ããŒãªã„ã‹ã‚‰
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top