is using tons of stl a correct method?

Discussion in 'C++' started by rokia, Jul 26, 2004.

  1. rokia

    rokia Guest

    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?
     
    rokia, Jul 26, 2004
    #1
    1. Advertising

  2. rokia

    Phlip Guest

    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/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Jul 26, 2004
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:NTZMc.1900$...
    > 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/bin/view/Main/TestFirstUserInterfaces
    >
    >
    >
    >
     
    Dave Townsend, Jul 26, 2004
    #3
  4. rokia

    rokia Guest

    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.:)
     
    rokia, Jul 26, 2004
    #4
  5. rokia

    rokia Guest

    thanks.

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



    "Dave Townsend" <> дÈëÓʼþ
    news:...
    > 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" <> wrote in message
    > news:NTZMc.1900$...
    > > 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/bin/view/Main/TestFirstUserInterfaces
    > >
    > >
    > >
    > >

    >
    >
     
    rokia, Jul 26, 2004
    #5
  6. I meant adhoc development of data structures (d/s).


    "rokia" <> wrote in message
    news:ce1sft$1606$99.com...
    > thanks.
    >
    > and what does 'home-brewed d/s' mean ?
    >
    >
    >
    > "Dave Townsend" <> дÈëÓʼþ
    > news:...
    > > 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" <> wrote in message
    > > news:NTZMc.1900$...
    > > > 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/bin/view/Main/TestFirstUserInterfaces
    > > >
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Dave Townsend, Jul 26, 2004
    #6
  7. 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 in message
    news:ce1s6o$15te$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.:)
    >
    >
    >
     
    Dave Townsend, Jul 26, 2004
    #7
  8. rokia

    rokia Guest

    yeah, I know this.

    I want to know which way is the better one?

    "Dave Townsend" <> дÈëÓʼþ
    news:...
    > 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 in message
    > news:ce1s6o$15te$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.:)
    > >
    > >
    > >

    >
    >
     
    rokia, Jul 26, 2004
    #8
  9. rokia

    Phlip Guest

    [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/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Jul 26, 2004
    #9
  10. rokia

    Phlip Guest

    Phlip, Jul 26, 2004
    #10
  11. rokia

    Phlip Guest

    Phlip, Jul 26, 2004
    #11
  12. On Mon, 26 Jul 2004 13:19:42 +0800, rokia <>
    wrote:

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


    Well of course it depends on what you want to do.

    But certainly I would avoid pointers most of the time. And even when I
    wanted pointers I would usually use smart pointers not raw pointers.

    john
     
    John Harrison, Jul 26, 2004
    #12
  13. rokia

    rokia Guest

    sorry for my poor english.

    I didn't got what you do mean.

    can you give me a simple answer?

    Yes or NO?
    Should I use list<A> or List<A*> ??
     
    rokia, Jul 26, 2004
    #13
  14. rokia

    rokia Guest

    Thanks A lot! :)



    "Phlip" <> дÈëÓʼþ
    news:6N0Nc.18$...
    > [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/bin/view/Main/TestFirstUserInterfaces
    >
    >
     
    rokia, Jul 26, 2004
    #14
  15. [snips]

    On Mon, 26 Jul 2004 05:38:10 +0000, Phlip wrote:

    > 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,


    Simple local testing reveals that inserting objects into a vector via
    push_back is approximately 8 times slower than simply storing them into an
    array and a simple loop and increment ( for i = 0 to nelems; inc elem )
    runs a good three times slower with vectors.

    Speed isn't the key to the STL. The fact that it provides reasonable
    speed, plus a very rich set of operations and relieves the coder from
    managing all the gory details seems a little more significant.
     
    Kelsey Bjarnason, Jul 26, 2004
    #15
  16. rokia

    Phlip Guest

    Kelsey Bjarnason wrote:

    > Phlip wrote:
    >
    > > 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,

    >
    > Simple local testing reveals that inserting objects into a vector via
    > push_back is approximately 8 times slower than simply storing them into an
    > array and a simple loop and increment ( for i = 0 to nelems; inc elem )
    > runs a good three times slower with vectors.


    Set the capacity first. That's cognitively the same thing you do with a
    fixed-size array. You compared a vector to calling new over and over again.

    > Speed isn't the key to the STL. The fact that it provides reasonable
    > speed, plus a very rich set of operations and relieves the coder from
    > managing all the gory details seems a little more significant.


    Speed is the key. Vendors may implement containers however they see fit, but
    each container type has a performance profile guaranteed to match an
    equivalent "manual" data structure. vector iterates as quickly as an array,
    and accesses randomly as quickly as an array. list can delete a middle
    element as quickly as a linked list can pull out an inner node. Etc.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Jul 26, 2004
    #16
  17. rokia

    J. Campbell Guest

    "Phlip" <> wrote in message news:<Bs5Nc.2639$>...
    > Kelsey Bjarnason wrote:
    >
    > > Phlip wrote:
    > >
    > > > 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,

    > >
    > > Simple local testing reveals that inserting objects into a vector via
    > > push_back is approximately 8 times slower than simply storing them into an
    > > array and a simple loop and increment ( for i = 0 to nelems; inc elem )
    > > runs a good three times slower with vectors.

    >
    > Set the capacity first. That's cognitively the same thing you do with a
    > fixed-size array. You compared a vector to calling new over and over again.
    >
    > > Speed isn't the key to the STL. The fact that it provides reasonable
    > > speed, plus a very rich set of operations and relieves the coder from
    > > managing all the gory details seems a little more significant.

    >
    > Speed is the key. Vendors may implement containers however they see fit, but
    > each container type has a performance profile guaranteed to match an
    > equivalent "manual" data structure. vector iterates as quickly as an array,
    > and accesses randomly as quickly as an array. list can delete a middle
    > element as quickly as a linked list can pull out an inner node. Etc.
    >
    > --
    > Phlip


    Hi guys. I read this thread with interest. I program as a hobby and
    migrated to C++ from BASIC about 2 yrs ago and have been wondering
    about using STL. I find myself using arrays rather than vectors even
    when I need to put the data on the heap, although I don't advocate
    others do the same...for me it just seems simpler. As an example of
    where I do this, I have a data-holding class that over-writes the
    memory (for security reasons, using memset()) in the destructor before
    releasing the memory. When I was designing the class, I couldn't
    think of an easy way to get a vector to do the same. Does this
    particular requirement (that no data remains in ram once it is
    released) necessitate the use of arrays, or can the same be achieved
    using vectors?

    The data size is unknown at compile time, so the class goes through
    the rigmarole of newing and deleting itself upon initiation, copy, and
    destruction.

    Thanks for this helpful thread.
     
    J. Campbell, Jul 26, 2004
    #17

  18. > As an example of
    > where I do this, I have a data-holding class that over-writes the
    > memory (for security reasons, using memset()) in the destructor before
    > releasing the memory. When I was designing the class, I couldn't
    > think of an easy way to get a vector to do the same. Does this
    > particular requirement (that no data remains in ram once it is
    > released) necessitate the use of arrays, or can the same be achieved
    > using vectors?


    Assuming your vector is called vec

    memset(&vec[0], 0, vec.size()*sizeof vec[0]);

    The trick is that &vec[0] can be used to get the address of the first vector
    element (and also that the C++ standard guarantees that a vector elements
    occupy contiguous memory).

    john
     
    John Harrison, Jul 26, 2004
    #18
  19. rokia

    Joe C Guest

    "John Harrison" <> wrote in message
    news:...
    >
    > > As an example of
    > > where I do this, I have a data-holding class that over-writes the
    > > memory (for security reasons, using memset()) in the destructor before
    > > releasing the memory. When I was designing the class, I couldn't
    > > think of an easy way to get a vector to do the same. Does this
    > > particular requirement (that no data remains in ram once it is
    > > released) necessitate the use of arrays, or can the same be achieved
    > > using vectors?

    >
    > Assuming your vector is called vec
    >
    > memset(&vec[0], 0, vec.size()*sizeof vec[0]);
    >
    > The trick is that &vec[0] can be used to get the address of the first

    vector
    > element (and also that the C++ standard guarantees that a vector elements
    > occupy contiguous memory).
    >
    > john
    >
    >


    Thanks John. What about if the vector reallocates itself (ie the data is
    concatinable)? Is there a way to make certain that the pre-reallocated
    vector is over-written?

    Thanks for the reply.

    Joe
     
    Joe C, Jul 26, 2004
    #19
  20. Joe C wrote:

    > "John Harrison" <> wrote in message
    > news:...
    >
    >>> As an example of
    >>>where I do this, I have a data-holding class that over-writes the
    >>>memory (for security reasons, using memset()) in the destructor before
    >>>releasing the memory. When I was designing the class, I couldn't
    >>>think of an easy way to get a vector to do the same. Does this
    >>>particular requirement (that no data remains in ram once it is
    >>>released) necessitate the use of arrays, or can the same be achieved
    >>>using vectors?

    >>
    >>Assuming your vector is called vec
    >>
    >>memset(&vec[0], 0, vec.size()*sizeof vec[0]);
    >>
    >>The trick is that &vec[0] can be used to get the address of the first

    >
    > vector
    >
    >>element (and also that the C++ standard guarantees that a vector elements
    >>occupy contiguous memory).

    >
    > Thanks John. What about if the vector reallocates itself (ie the data is
    > concatinable)? Is there a way to make certain that the pre-reallocated
    > vector is over-written?


    You could use std::vector::reserve() to prevent reallocations. Or you
    could use a different allocator with the vector; one that clears memory
    before deallocating it. Such an allocator could also be used with other
    container classes.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
     
    Peter van Merkerk, Jul 26, 2004
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bogdan Nedelcu

    ASP.NET compilation generates tons of DLLs

    Bogdan Nedelcu, Jan 25, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    369
    Lionel LASKE
    Jan 25, 2005
  2. Quentin Crain
    Replies:
    0
    Views:
    281
    Quentin Crain
    May 5, 2005
  3. saifullah
    Replies:
    0
    Views:
    317
    saifullah
    Sep 29, 2008
  4. palewire
    Replies:
    1
    Views:
    228
    Paul Rubin
    Jul 7, 2009
  5. David Heinemeier Hansson
    Replies:
    1
    Views:
    104
    Bill Guindon
    Mar 7, 2005
Loading...

Share This Page