Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Best STL container to use for single pass-through operations

Reply
Thread Tools

Best STL container to use for single pass-through operations

 
 
Josh Mcfarlane
Guest
Posts: n/a
 
      12-06-2005
Quick question:

If I'm creating a container in logical order (1st node added, 2nd node
added, etc) and only reading in a single forward passthrough, what STL
container would be best optimized for this?

Thanks,
Josh McFarlane

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-06-2005
Josh Mcfarlane wrote:
> Quick question:
>
> If I'm creating a container in logical order (1st node added, 2nd node
> added, etc) and only reading in a single forward passthrough, what STL
> container would be best optimized for this?


std::deque, probably.

V
 
Reply With Quote
 
 
 
 
Calum Grant
Guest
Posts: n/a
 
      12-06-2005
Josh Mcfarlane wrote:
> Quick question:
>
> If I'm creating a container in logical order (1st node added, 2nd node
> added, etc) and only reading in a single forward passthrough, what STL
> container would be best optimized for this?
>
> Thanks,
> Josh McFarlane
>


std::vector would be the default choice.
 
Reply With Quote
 
Augusto KhaoticMind da Silva
Guest
Posts: n/a
 
      12-06-2005
I'd go with a std::list.
If you know how many elements you have from the begining than
std::vector can be a good option, since all memory would be
preallocated and the element access would be in a constant time.
If thats not the case, then i'd stick with a list, since vectors would
add some resizing overhead, when you add one element more than their
capacity.

 
Reply With Quote
 
Axter
Guest
Posts: n/a
 
      12-07-2005

Augusto KhaoticMind da Silva wrote:
> I'd go with a std::list.
> If you know how many elements you have from the begining than
> std::vector can be a good option, since all memory would be
> preallocated and the element access would be in a constant time.
> If thats not the case, then i'd stick with a list, since vectors would
> add some resizing overhead, when you add one element more than their
> capacity.


std::list should be your last choice. In general, it gives you the
worse access performance.

std::vector should be your default and preferred container to use, if
you're only adding and removing from the end of the container.

If you're adding and removing from the beginning and from the end of
the container, then use std::deque.

Only use std::list if you're adding and removing from the center of the
container.
Moreover, even if you're adding and removing from the center of the
container, experts like Herb Sutter, still recommend to consider using
std::vector if you're only infrequently adding/removing from/to the
center.

 
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
container inside container in stl wolverine C++ 2 07-24-2006 03:08 PM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
stand-alone JMS, other JDBC operations, and transactions ( ActiveMQ + JOTM + JDBC operations ) Jesus M. Salvo Jr. Java 2 02-11-2006 06:33 PM
What's the best way to hide the particular STL container that my class uses? Ed C++ 3 04-04-2005 01:17 PM
STL: container's values setup by another container Maitre Bart C++ 2 02-11-2004 12:11 AM



Advertisments