cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
Array vs List
Array vs List
Jul 29, 2014 at 9:11pm UTC
Richard 4234
(60)
Should I use an array or list?(I don't have specific code)
When should I use array, when should I use list?
What are the advantages and disadvantages for using an array and a list?
Jul 29, 2014 at 9:21pm UTC
Smac89
(1727)
You have to be more specific. What type of list?
http://en.wikipedia.org/wiki/List_(abstract_data_type)
And when you say array, do you mean a fixed size array (std::array<T>) or a dynamic array (std::vector<T>)?
Jul 29, 2014 at 9:22pm UTC
Richard 4234
(60)
Double linked list(std::list), and std::array.
Last edited on
Jul 29, 2014 at 9:37pm UTC
Jul 29, 2014 at 9:34pm UTC
Smac89
(1727)
Well that is easy enough.
Double linked list
Advantage:
- Dynamic growth
Disadvantage:
- Access to any element apart from the first and last takes O(N) time
Example: std::list<T> -
http://www.cplusplus.com/reference/list/list/
std::array
Advantage:
- O(1) access to any element
Disadvantage:
- Fixed size, enforced by compiler so can't grow at runtime
Example:
http://www.cplusplus.com/reference/array/array/
There might be more, but this is just the basic advantage/disadvantage one has to consider when deciding which to pick
Jul 29, 2014 at 9:38pm UTC
Richard 4234
(60)
Thank you very much!
Topic archived. No new replies allowed.