Why should I use simple arrays rather than container classes?

largest_prime_is_463035818

What (if anything) can be done more efficiently with a array rather than a container?

I recently learned about C++ standard container classes. They have clear advantages and solve common problems with C-style arrays. The FAQs list on "why arrays are evil" can be summarized loosely like this:

1. subscripts are not checked
2. often it is required to allocate memory from the heap
3. not easy to insert elements in the middle
4. always passed as reference

I guess there are many cases, where one can live with these disadvantages. However, I am a bit puzzled about the question, what is it that can be done more efficient / easier with arrays rather than with containers? Or is there actually nothing like that and I really should not care about arrays anymore?

πάντα ῥεῖ

"However, I am a bit puzzled about the question, what is it that can be done more efficient / easier with arrays rather than with containers?"

Well, if you're referring to c-style arrays, with the current c++ standard, there's nothing left about the disadvantages of the classic standard c++ container classes (like e.g. std::vector) IMHO. These have a dependency to have allocatable memory (with new()), and that could well be be a restriction for your current (OS/bare metal) environment, as not being available out of the box.


The current standard provides std::array, that works without dynamic memory allocation needs, but fulfills all of the claims:

"1. subscripts are not checked"

std::array does subscript checking

"2. often it is required to allocate memory from the heap"

It's the client's choice where it's actually allocated with std::array. std::vector suffices anyway for doing this.

"3. not easy to insert elements in the middle"

Well, that's one point not supported well by std::array out of the box. But again, std::vector and other container classes support this well (as long your environment supports dynamic memory allocation)

"4. always passed as reference"

std::array supports passing by reference well, and much better than it can (can't) be achieved with c-style arrays.


Though there may be specialized cases for e.g. reusable object instance pools, or flyweight object instances, you might want to solve using the placement new() operator. Implementations for such solutions, usually would involve you're going to operate on raw c-style arrays.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why should we use classes rather than records, or vice versa?

From Java

Why should I use a pointer rather than the object itself?

From Dev

Should I use an iframe rather than ajax?

From Dev

Why Should I use virtual base classes?

From Dev

When to use templates rather than derived classes

From Dev

Why Java supports arrays of arrays, rather than multidimensional arrays?

From Dev

Why should I be making my page objects instantiated rather than static?

From Dev

Why should I pass make -j an argument? (rather than leave it blank)

From Dev

Why should I structure my HTML emails with tables rather than divs?

From Java

What is phtml, and when should I use a .phtml extension rather than .php?

From Dev

Cython: Should I use np.float_t rather than double for typed memory views

From Dev

In which cases, I should use Capture rather than an object in Unit Test with EasyMock?

From Dev

Why do some CLIs use ` and ' rather than ‛ and ’?

From Dev

Why use TimeSpan.CompareTo() rather than < > or =

From Dev

Why use a function rather than a reference to member?

From Dev

Why use ImageIcon rather than Image?

From Dev

Why to use ByteArrayInputStream rather than byte[] in Java

From Dev

Why use "nohup &" rather than "exec &"

From Dev

Why do some CLIs use ` and ' rather than ‛ and ’?

From Dev

Why use Python format rather than slicing?

From Dev

In the regularization,why we use θ^2 rather than θ?

From Dev

Why use regex finditer() rather than findall()

From Dev

Array of arrays - why I have one more level of Arrays than I think I should?

From Dev

Array of arrays - why I have one more level of Arrays than I think I should?

From Dev

Why would I ever use a traditional array rather than an ArrayList object?

From Dev

Why shouldn't I use shell_exec rather than output buffering to 'include' a file into a variable?

From Dev

Why should I use anonymous classes in Android instead of class redefinition?

From Dev

Use phalcon classes in base controller rather than on individual controllers

From Dev

When possible, should a programmer always use < rather than <=?

Related Related

  1. 1

    Why should we use classes rather than records, or vice versa?

  2. 2

    Why should I use a pointer rather than the object itself?

  3. 3

    Should I use an iframe rather than ajax?

  4. 4

    Why Should I use virtual base classes?

  5. 5

    When to use templates rather than derived classes

  6. 6

    Why Java supports arrays of arrays, rather than multidimensional arrays?

  7. 7

    Why should I be making my page objects instantiated rather than static?

  8. 8

    Why should I pass make -j an argument? (rather than leave it blank)

  9. 9

    Why should I structure my HTML emails with tables rather than divs?

  10. 10

    What is phtml, and when should I use a .phtml extension rather than .php?

  11. 11

    Cython: Should I use np.float_t rather than double for typed memory views

  12. 12

    In which cases, I should use Capture rather than an object in Unit Test with EasyMock?

  13. 13

    Why do some CLIs use ` and ' rather than ‛ and ’?

  14. 14

    Why use TimeSpan.CompareTo() rather than < > or =

  15. 15

    Why use a function rather than a reference to member?

  16. 16

    Why use ImageIcon rather than Image?

  17. 17

    Why to use ByteArrayInputStream rather than byte[] in Java

  18. 18

    Why use "nohup &" rather than "exec &"

  19. 19

    Why do some CLIs use ` and ' rather than ‛ and ’?

  20. 20

    Why use Python format rather than slicing?

  21. 21

    In the regularization,why we use θ^2 rather than θ?

  22. 22

    Why use regex finditer() rather than findall()

  23. 23

    Array of arrays - why I have one more level of Arrays than I think I should?

  24. 24

    Array of arrays - why I have one more level of Arrays than I think I should?

  25. 25

    Why would I ever use a traditional array rather than an ArrayList object?

  26. 26

    Why shouldn't I use shell_exec rather than output buffering to 'include' a file into a variable?

  27. 27

    Why should I use anonymous classes in Android instead of class redefinition?

  28. 28

    Use phalcon classes in base controller rather than on individual controllers

  29. 29

    When possible, should a programmer always use < rather than <=?

HotTag

Archive