0

Possible Duplicate:
Advantages of using arrays instead of std::vector?

What are the main advantages/disadvantages of array and vector in C++? I usually find that vectors are easier to use due to their dynamic nature but sometimes it seems like arrays are faster with less overhead. Are there any other significant advantages? I usually am not sure when to use one over the other, depending.

Community
  • 1
  • 1
AnovaVariance
  • 2,415
  • 2
  • 14
  • 6
  • 3
    There are lots of duplicates of this: http://stackoverflow.com/search?q=%5Bc%2B%2B%5D+vector+vs.+array. – Oliver Charlesworth Jul 17 '12 at 13:58
  • 1
    "Sometimes it *seems*"? "with less overhead"? Are you sure? What operation can arrays do with less overhead than a vector? You may want to buy a better compiler. I am all out of nickels, though. – R. Martinho Fernandes Jul 17 '12 at 13:58
  • @R.MartinhoFernandes Construction and destruction. If you have a struct `Point3D`, with an array/vector of three `double`, something like `std::vector v(1000000)` will be significantly faster if `Point3D` contains a `double []` rather than a `vector` initialized with 3. – James Kanze Jul 17 '12 at 14:01

2 Answers2

1

The most obvious reason to prefer an array is to achieve static initialization of a variable at namespace scope; static initialization means no order of initialization issues, ever. (If you have C++11, std::array provides this as well.)

The other main reason is to allow the compiler to determine the size according to the number of initializers, without you having to count them.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
0

(in 2012) There are no reason to use C type arrays in C++.

Faster, cleaner, safer.

Max
  • 3,128
  • 1
  • 24
  • 24