Hello there :)

I have recently started learning lists in C++, and I just do not understand what is the advantage of lists over arrays? I only see disadvantages, like: they're harder to implement, harder to manage, a little bit confusing, and so on...

So, i know i'm a n00b in c++ lists for now, but can you please show me some concrete examples where lists can be more useful than arrays?

Recommended Answers

All 4 Replies

One advantage of lists that I can think of is that it is often easier to rearrange the data in a list because you are working with pointers. That is, if you have a list of objects and you want to change the order of the data, you can simply change where each pointer points to, rather than having to copy the array to a new array in the specific order that you want. Thus, sorted lists are easier to implement than sorted arrays.

Lists are harder to implement initially. But just compare sorting, searching, inserting an element, deleting an element etc. for both arrays and lists. You'd realise that lists are better. What is more, lists are dynamic.

>I just do not understand what is the advantage of lists over arrays?
Insertion and deletion are either equal to or better than arrays in terms of theoretic efficiency. Growing and shrinking a list falls out of the implementation, whereas you'd have to specially design a simulated array to change the size at runtime.

>Thus, sorted lists are easier to implement than sorted arrays.
I have to disagree with this one. The pointer surgery required to restructure a linked list is tricky and difficult to get right unless you have a lot of experience with it. Sorted lists are theoretically more efficient than sorted arrays because you don't have to worry about shifting around the other items.

>You'd realise that lists are better.
Not always. Assuming "list" means "linked list", there are advantages and disadvantages of both lists and arrays. The situation that you need to use it in is the defining factor in which one to choose.

>What is more, lists are dynamic.
Arrays can be dynamic as well.

>You'd realise that lists are better.
Not always. Assuming "list" means "linked list", there are advantages and disadvantages of both lists and arrays. The situation that you need to use it in is the defining factor in which one to choose.

I agree. Exactly what I had meant to convey...

I'm sorry for this one.

What is more, lists are dynamic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.