Блог пользователя the41

Автор the41, история, 4 года назад, По-английски

Hello,

I recently switched from java to C++ and encountered a question. I notice that everybody seems to use C-style arrays over C++ arrays. To me, it seems that C++ arrays, being easier to pass in functions and with the inclusion of bound checks, would be better, but this does not appear to be the case. Is it because of faster typing or tradition or why does everyone still use C-style arrays? Sorry if the question seems very basic; I searched for a while but could not find an answer.

To my understanding:

int a[3]; //C-style
array<int, 3> a; //C++ style

Thank you!

Теги c++
  • Проголосовать: нравится
  • +12
  • Проголосовать: не нравится

»
4 года назад, # |
Rev. 2   Проголосовать: нравится -14 Проголосовать: не нравится

My answer was wrong so I edited it out

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +9 Проголосовать: не нравится

    I believe C++ arrays from C++11 are implemented using arrays and not vectors. They are just a wrapper class and so should be nearly as fast as C-style arrays.

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +6 Проголосовать: не нравится

    I always use vector instead of array since it fits with C++ STL more naturally. I don't think I've ever had a problem with vectors because of things that you've mentioned. They're very minor IMO.

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится +1 Проголосовать: не нравится

      Vectors are usually very slow when you work with small size vectors a lot of times. I think that there's almost no difference if you're working with large vectors.

»
4 года назад, # |
  Проголосовать: нравится +9 Проголосовать: не нравится

with the inclusion of bound checks

This is a terrible idea in CP. Why don't you validate the input in your code while you're at it?

You need to learn to live dangerously.