Is it bad practice to use multi-dimensional arrays in C/C++?

前端 未结 9 822
北海茫月
北海茫月 2021-02-07 04:46

Some programmers seem to violently hate them, while others seem to think they\'re fine. I know that anything that can be done to a multi-dimensional array can also be done to a

相关标签:
9条回答
  • 2021-02-07 05:31

    If multidimensional index computation bugs you, std::valarray with std::slice is the standard abstraction.

    0 讨论(0)
  • 2021-02-07 05:33

    Well, in C++ I dislike multidimensional arrays because they should be replaced with std::vector<std::vector<t> >. They're also particularly important if you want to represent a std::vector<std::basic_string<t> >.

    Multidimensional arrays are so simple a primitive I'm suprised most would care. However, a design that uses a single dimension is probably better than one using multiple dimensions, all other things being equal.

    0 讨论(0)
  • 2021-02-07 05:35

    There are following advantages of multi-dimensional arrays over Vector<Vector<>>:

    • They are easy to understand.
    • Searching and sorting of elements can be done very easily.
    • They are compatible with C.
    • They are easy to type.
    0 讨论(0)
提交回复
热议问题