Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > is using tons of stl a correct method?

Reply
Thread Tools

is using tons of stl a correct method?

 
 
rokia
Guest
Posts: n/a
 
      07-26-2004
in a project, I use many,many stl such as stack,list,vctor etc.

somewhere the vector's size is more than 2K. is this a efficient way?


 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      07-26-2004
rokia wrote:

> in a project, I use many,many stl such as stack,list,vctor etc.
>
> somewhere the vector's size is more than 2K. is this a efficient way?


Using robust Standard Library gizmos is infinitely more efficient, in terms
of programming time, than using raw arrays, linked lists, 'new', etc. If you
use those, you will endlessly reinvent low-level details that Standard
Library things, and risk bugs as you reinvent. Using STL containers makes
C++ as cognitively efficient as a "high-level" language.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces




 
Reply With Quote
 
 
 
 
Dave Townsend
Guest
Posts: n/a
 
      07-26-2004
Yes, I concur. I've recently been able to use STL exclusively rather
than C-arrays and home-brewed d/s, I've had very few crashes and
clean up is a breeze - its done for me! Thank you Alex Stepanov, you da
man!!



"Phlip" <(E-Mail Removed)> wrote in message
news:NTZMc.1900$(E-Mail Removed) m...
> rokia wrote:
>
> > in a project, I use many,many stl such as stack,list,vctor etc.
> >
> > somewhere the vector's size is more than 2K. is this a efficient way?

>
> Using robust Standard Library gizmos is infinitely more efficient, in

terms
> of programming time, than using raw arrays, linked lists, 'new', etc. If

you
> use those, you will endlessly reinvent low-level details that Standard
> Library things, and risk bugs as you reinvent. Using STL containers makes
> C++ as cognitively efficient as a "high-level" language.
>
> --
> Phlip
> http://industrialxp.org/community/bi...UserInterfaces
>
>
>
>



 
Reply With Quote
 
rokia
Guest
Posts: n/a
 
      07-26-2004
thanks for your reply.

another question:

when you have a class A. will you push_back A to a list? or just the
pointer of A?

I mean which is the right one below:

1: class A;
list<A> la;
A a,b,c;
la.push_back(a); la.push_back(b),la.push_back(c);

2:class A;
list<A*> la;
A *pa;
A *pb;
A *pc;
la.push_back(pa);la.push_back(pb);la.push_back(pc) ;

and which method should I use If THERE ARE 5000 class A OR MORE?

thanks.



 
Reply With Quote
 
rokia
Guest
Posts: n/a
 
      07-26-2004
thanks.

and what does 'home-brewed d/s' mean ?



"Dave Townsend" <(E-Mail Removed)> 写入邮件
news:(E-Mail Removed)...
> Yes, I concur. I've recently been able to use STL exclusively rather
> than C-arrays and home-brewed d/s, I've had very few crashes and
> clean up is a breeze - its done for me! Thank you Alex Stepanov, you da
> man!!
>
>
>
> "Phlip" <(E-Mail Removed)> wrote in message
> news:NTZMc.1900$(E-Mail Removed) m...
> > rokia wrote:
> >
> > > in a project, I use many,many stl such as stack,list,vctor etc.
> > >
> > > somewhere the vector's size is more than 2K. is this a efficient way?

> >
> > Using robust Standard Library gizmos is infinitely more efficient, in

> terms
> > of programming time, than using raw arrays, linked lists, 'new', etc. If

> you
> > use those, you will endlessly reinvent low-level details that Standard
> > Library things, and risk bugs as you reinvent. Using STL containers

makes
> > C++ as cognitively efficient as a "high-level" language.
> >
> > --
> > Phlip
> >

http://industrialxp.org/community/bi...UserInterfaces
> >
> >
> >
> >

>
>



 
Reply With Quote
 
Dave Townsend
Guest
Posts: n/a
 
      07-26-2004

I meant adhoc development of data structures (d/s).


"rokia" <(E-Mail Removed)> wrote in message
news:ce1sft$1606$(E-Mail Removed)99.com...
> thanks.
>
> and what does 'home-brewed d/s' mean ?
>
>
>
> "Dave Townsend" <(E-Mail Removed)> 写入邮件
> news:(E-Mail Removed)...
> > Yes, I concur. I've recently been able to use STL exclusively rather
> > than C-arrays and home-brewed d/s, I've had very few crashes and
> > clean up is a breeze - its done for me! Thank you Alex Stepanov, you

da
> > man!!
> >
> >
> >
> > "Phlip" <(E-Mail Removed)> wrote in message
> > news:NTZMc.1900$(E-Mail Removed) m...
> > > rokia wrote:
> > >
> > > > in a project, I use many,many stl such as stack,list,vctor etc.
> > > >
> > > > somewhere the vector's size is more than 2K. is this a efficient

way?
> > >
> > > Using robust Standard Library gizmos is infinitely more efficient, in

> > terms
> > > of programming time, than using raw arrays, linked lists, 'new', etc.

If
> > you
> > > use those, you will endlessly reinvent low-level details that Standard
> > > Library things, and risk bugs as you reinvent. Using STL containers

> makes
> > > C++ as cognitively efficient as a "high-level" language.
> > >
> > > --
> > > Phlip
> > >

> http://industrialxp.org/community/bi...UserInterfaces
> > >
> > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Dave Townsend
Guest
Posts: n/a
 
      07-26-2004
STL containers work by making copies of objects, so push_back(a)
makes a copy of a in the container. When you access a, say by
vector at(), it gives you a copy of the object. Make sure that
you have the appropriate constructors implemented however, otherwise
you'll end up with garbage...

You can also store pointers in the STL container, but you have
to declare the container as a container of pointers, ie, list<A*> rather
than list<A>


"rokia" <(E-Mail Removed)> wrote in message
news:ce1s6o$15te$(E-Mail Removed)99.com...
> thanks for your reply.
>
> another question:
>
> when you have a class A. will you push_back A to a list? or just the
> pointer of A?
>
> I mean which is the right one below:
>
> 1: class A;
> list<A> la;
> A a,b,c;
> la.push_back(a); la.push_back(b),la.push_back(c);
>
> 2:class A;
> list<A*> la;
> A *pa;
> A *pb;
> A *pc;
> la.push_back(pa);la.push_back(pb);la.push_back(pc) ;
>
> and which method should I use If THERE ARE 5000 class A OR MORE?
>
> thanks.
>
>
>



 
Reply With Quote
 
rokia
Guest
Posts: n/a
 
      07-26-2004
yeah, I know this.

I want to know which way is the better one?

"Dave Townsend" <(E-Mail Removed)> 写入邮件
news:(E-Mail Removed)...
> STL containers work by making copies of objects, so push_back(a)
> makes a copy of a in the container. When you access a, say by
> vector at(), it gives you a copy of the object. Make sure that
> you have the appropriate constructors implemented however, otherwise
> you'll end up with garbage...
>
> You can also store pointers in the STL container, but you have
> to declare the container as a container of pointers, ie, list<A*> rather
> than list<A>
>
>
> "rokia" <(E-Mail Removed)> wrote in message
> news:ce1s6o$15te$(E-Mail Removed)99.com...
> > thanks for your reply.
> >
> > another question:
> >
> > when you have a class A. will you push_back A to a list? or just the
> > pointer of A?
> >
> > I mean which is the right one below:
> >
> > 1: class A;
> > list<A> la;
> > A a,b,c;
> > la.push_back(a); la.push_back(b),la.push_back(c);
> >
> > 2:class A;
> > list<A*> la;
> > A *pa;
> > A *pb;
> > A *pc;
> > la.push_back(pa);la.push_back(pb);la.push_back(pc) ;
> >
> > and which method should I use If THERE ARE 5000 class A OR MORE?
> >
> > thanks.
> >
> >
> >

>
>



 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      07-26-2004
[top-post fixed - please take pride in your posts and clean each one]

Dave Townsend wrote:

> > STL containers work by making copies of objects, so push_back(a)
> > makes a copy of a in the container. When you access a, say by
> > vector at(), it gives you a copy of the object. Make sure that
> > you have the appropriate constructors implemented however, otherwise
> > you'll end up with garbage...
> >
> > You can also store pointers in the STL container, but you have
> > to declare the container as a container of pointers, ie, list<A*> rather
> > than list<A>


rokia wrote:

> yeah, I know this.
>
> I want to know which way is the better one?


Premature optimization is the root of all evil. STL wouldn't exist if it
weren't as fast as the alternative (raw arrays for std::vector, raw linked
lists for std::list, etc.).

What you gain by using STL is clean interfaces that are very easy to use
right and frequently hard to use wrong. That frees your time up to
concentrate on program logic.

The most important resource to optimize is programmer time. If you write
clean code (and unit tests on each feature), you will have time to then
profile your code and see if it really is slow. The odds are very good that
your code will perform acceptably with obvious implementations.

> > > and which method should I use If THERE ARE 5000 class A OR MORE?


The only only only way to tell is put 5,000 class A objects in and see.
Different STLs have different performance profiles, but many other aspects
of your code, even the vaguarities of your CPU cache and pipelining, can
affect performance.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      07-26-2004
rokia wrote:

> another question:
>
> when you have a class A. will you push_back A to a list? or just the
> pointer of A?


If A can have derived classes, push a shared pointer (such as the smart
pointers at www.boost.org).

If A can't have derived classes, push it in by copy.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HSSI Port, larcoms Orion 4000, Tons of abort/crc errors hallcommunications@gmail.com Cisco 0 04-15-2005 01:32 PM
ASP.NET compilation generates tons of DLLs Bogdan Nedelcu ASP .Net 1 01-25-2005 08:37 AM
I really need this book in PDF format. I have tons of stuff to trade! Phillip Cisco 0 11-01-2004 03:36 AM
Tons of Logging Data jt Cisco 5 02-16-2004 08:04 PM
Tons of Spam - HELP Wallygator Computer Support 21 09-24-2003 09:20 PM



Advertisments