Just out of curiosity:
When would using std::list be more efficient / effective than using
other containers such as vector, deque, etc?
As far as I'm aware, list doesn't appear to be specialized for
anything.
Thanks,
Josh McFarlane 44 3872
Hi
Josh Mcfarlane wrote: Just out of curiosity:
When would using std::list be more efficient / effective than using other containers such as vector, deque, etc?
As far as I'm aware, list doesn't appear to be specialized for anything.
Think about the complexity of erasing or inserting elements at arbitrary
positions. Also think about what happens to iterators when doing so.
Markus
Markus Moll wrote: Hi
Josh Mcfarlane wrote:
Just out of curiosity:
When would using std::list be more efficient / effective than using other containers such as vector, deque, etc?
As far as I'm aware, list doesn't appear to be specialized for anything.
Think about the complexity of erasing or inserting elements at arbitrary positions. Also think about what happens to iterators when doing so.
Splicing subvectors is also rather tricky ;)
HTH,
Michiel Salters
The runtime of list for
deletion and insertion operation is better than vectors.
When you delete anything from vector, all the iterators after that item
must be reassigned. no such thing wtih list.
Josh Mcfarlane wrote: Just out of curiosity:
When would using std::list be more efficient / effective than using other containers such as vector, deque, etc?
As far as I'm aware, list doesn't appear to be specialized for anything.
Thanks, Josh McFarlane
constant time insertion and deletion at any point in the list. (i.e.
regardless of the current size of the list).
list would seem to be at its most optimal (compared to other
containers) when you are a large list of relatively small objects
rather than a small list of large objects (latter can be implemented
using smart-pointers so the objects are not so large after all), and
you are inserting/deleting in the middle.
"Josh Mcfarlane" <da*****@gmail. com> wrote: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc?
std::list is best any time you have one or more of the
following situations:
1. Wildly variable amount of data.
2. Data needs to be inserted or deleted in the middle.
3. Data needs to be sorted frequently.
As far as I'm aware, list doesn't appear to be specialized for anything.
It's optimized for insertions and sorting. Since the
order of elements is based on links, elements don't
have to be moved to sort or insert, which drastically
speeds those processes.
Try doing that with a vector! Oh, lordy...
You'd need to move thousands of bytes of data around,
possibly repeatedly re-allocating huge memory blocks.
Very inefficient.
But with std::list, those things are fast and efficient,
and don't require re-allocation.
--
Robbie Hatley
Tustin, CA, USA
lone wolf intj at pac bell dot net
home dot pac bell dot net slant earnur slant
pepp wrote: The runtime of list for deletion and insertion operation is better than vectors.
When you delete anything from vector, all the iterators after that item must be reassigned. no such thing wtih list.
That's not entirely correct.
This only happens when deleting from the center or beginning of the
vector.
deleting from the end of the vector does not cause this.
Josh Mcfarlane wrote: Just out of curiosity:
When would using std::list be more efficient / effective than using other containers such as vector, deque, etc?
As far as I'm aware, list doesn't appear to be specialized for anything.
Thanks, Josh McFarlane
You should use vector as your default container.
Normally, std::list is rarely the best choice. In most requirements,
std::list will perform worse, or the same as std::vector.
You should consider using std::list if you have a requirement that
performs many inserts and/or deletes from the center of the container.
If you only perform a few insertions and/or deletions from the center,
then you should still prefer to use std::vector or std::deque.
Sorting requirements should not be a motivating factor for using
std::list, because you're more then likely using the wrong set of
containers if you have a (repeat) sorting requirement.
If you have a requirement that needs to do a lot of sorting, then you
should consider using std::map or std::set instead.
In general, std::list should be your least used container.
Robbie Hatley wrote: "Josh Mcfarlane" <da*****@gmail. com> wrote:
When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? std::list is best any time you have one or more of the following situations:
1. Wildly variable amount of data.
Should not be a factor in considering using std::list instead of
std::vector
2. Data needs to be inserted or deleted in the middle.
This should be the primary factor, and is usually only important when
there are MANY inserts and deletes from the center.
3. Data needs to be sorted frequently.
If data needs to be frequently sorted, then consider using std::set or
std::map instead. As far as I'm aware, list doesn't appear to be specialized for anything. It's optimized for insertions and sorting. Since the order of elements is based on links, elements don't have to be moved to sort or insert, which drastically speeds those processes.
It's optimized for middle insertions. However, compare to std::vector
and std::deque, std::list is poorly optimized for end insertions. And
compare to std::deque, std::list is poorly optimized for beginning
insertions.
Try doing that with a vector! Oh, lordy... You'd need to move thousands of bytes of data around, possibly repeatedly re-allocating huge memory blocks. Very inefficient.
Only when inserting in the beginning or middle of the container.
Most experts, (like Herb Sutters and Scott Meyers) recommend using
std::vector as the default container.
The Official C++ standard also recommends using std::vector as the
default container.
In most container requirements, std::vector will out perform std::list.
Axter wrote: Josh Mcfarlane wrote: Just out of curiosity:
When would using std::list be more efficient / effective than using other containers such as vector, deque, etc?
As far as I'm aware, list doesn't appear to be specialized for anything.
Thanks, Josh McFarlane You should use vector as your default container.
Normally, std::list is rarely the best choice. In most requirements, std::list will perform worse, or the same as std::vector.
You should consider using std::list if you have a requirement that performs many inserts and/or deletes from the center of the container.
Or if you perform a lot of push_back operations and will be accessing
in a linear fassion 99% of the time or better. A list has to allocate
for a new node but it doesn't have to copy the whole list to the new
memory, it can just assign values. A vector on the other hand is
*almost* guaranteed to be contiguous memory and therefore will most
likely copy the entire contents upon a need to resize.
However, because of the lack of a random access iterator it can still
be more efficient to use a vector if you need random access. If you
are just running the whole list every time you access it then a list is
a very efficient implementation unless you know ahead of time exactly
how big to make your vector (or at least a good usual upper bound). If you only perform a few insertions and/or deletions from the center, then you should still prefer to use std::vector or std::deque.
Sorting requirements should not be a motivating factor for using std::list, because you're more then likely using the wrong set of containers if you have a (repeat) sorting requirement.
Not only that, but because std::list doesn't have a random access
iterator sorting a list will be considerably slower than a
vector...most of the time. There are rare cases when it might not be,
but usually you will find sort to work faster with a random access than
without.
If you have a requirement that needs to do a lot of sorting, then you should consider using std::map or std::set instead.
In general, std::list should be your least used container.
I don't agree. Every container serves a particular purpose. It could
be that in YOUR case most development domains seem to be least served
by the list container there are certainly a lot of cases when a list is
a very appropriate choice. The best thing is to understand the
implementations of the different containers and where those kind of
algorithms are best used. Take a class or read a book on data
structures and implement your own list, vector, map, etc...and then you
will be able to understand their value and when to use what. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Anton |
last post by:
Hello !
I am writing a small program and wondering whether this expression is
right?
std::list< std::string > strList
I mean that this is container into container and don't know the side
effects of this expression, that could crash my program. Please , if
you know any side effect will you explain me where is it ?
|
by: Dave |
last post by:
Hello all,
After perusing the Standard, I believe it is true to say that once you
insert an element into a std::list<>, its location in memory never changes.
This makes a std::list<> ideal for storing vertices of an arbitrary n-ary
tree where a vertex contain pointers to its parent / children. These parent
/ child vertices need to stay put if we've got pointers to them somewhere!
Am I correct in my assertion?
|
by: JustSomeGuy |
last post by:
I need to write an new class derived from the list class.
This class stores data in the list to the disk if an object
that is added to the list is over 1K in size.
What methods of the std stl list class must Ioverride in order for this
to work?
|
by: Glen Able |
last post by:
Without further ado, here's some code:
std::list<int> things;
things.push_back(1);
things.push_back(2);
things.push_back(3);
std::list<int>::iterator it;
int test;
|
by: PengYu.UT |
last post by:
Hi,
Suppose I have a list which contains pointers. I want the pointer got
by dereferencing the iterator be a pointer pointing to a const object.
But std::list<const T*>::const_iterator doens't give me this
capability. So I want std::list<T*>::iterator.
However, the container is of type std::list<T*>. How to get
std::list<const T*>::iterator?
| |
by: Markus Svilans |
last post by:
Hi,
There seems to be some functionality missing from the STL.
I am iterating through a linked list (std::list) using a reverse
iterator and attempting to erase certain items from the list. It is
important that I iterate through the list backwards, because the items
in it have to be processed in reverse order before erasing. However,
there does not appear to be an std::list::erase() method defined for
reverse iterators.
|
by: desktop |
last post by:
If I have a sorted std::list with 1.000.000 elements it takes 1.000.000
operations to find element with value = 1.000.000 (need to iterator
through the whole list).
In comparison, if I have a std::set with 1.000.000 element it will only
take approx lg 1.000.000 = 20 operations! Can it really be true that the
difference is a factor of 1.000.000/20 = 50.000 in this case?
|
by: Spoon |
last post by:
Hello,
Could someone explain why the following code is illegal?
(I'm trying to use a list of (C-style) arrays.)
#include <list>
typedef std::list < int foo_t;
int main()
{
int v = { 12, 34 };
|
by: isliguezze |
last post by:
template <class T>
class List {
public:
List();
List(const List&);
List(int, const T&);
void push_back(const T &);
void push_front(const T &);
void pop_back();
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |