Difference between vector object and c++11 array?

It's quite "hard" for me to understand the difference between those two.
If I'm correct with the c++11 array we can do:

 
  array<int, 3> test = {1, 2, 3};


But with using Vector we can not initialize it at the creation:

1
2
3
4
5
  vector<int> test2(3);

  test2[0] = 1;
  test2[1] = 2;
  test2[2] = 3;


Is this the only difference? I might be completely wrong, so I'd appreciate if somebody could try to explain it to me. Thank you :)
But with using Vector we can not initialize it at the creation:

If you're using a modern standard you can initialize a vector at creation just like the std::array.

std::vector<int> test3{2,34,4};

However the major difference between a std::array and a std::vector is that the array has it's size fixed at compile time a vector is dynamic and can be re-sized whenever needed. And realize that there are more differences between these two types.
there are really 3 main types. (I am leaving off pointers, that would be a 4th, and tuples, which I believe are built off std:array, may be though of as a 5th?).
C array:
int x[10];
this is just a location in memory, with 10 integers (10*sizeof(int) bytes). You can tap it with x[index] but it has no built in copy, is not aware of its own size. These are not used as much anymore, but you still see them. You cannot change its size at run-time.

std::array is an improved/OOP friendly version of the above. It has a built in assignment operator, knows its own size, and has a small # of handy operators (many of which are silly, like front, which is just array[0] via a function call), and you can pull the underlying C array out of it with data(), etc. It IS a class, under the hood. I have mixed feelings about it... for the most part, it seems like a heavy C array, giving up efficiency (microscopic, but its there) and space to babysit the programmer. Yes, it is more c++ish, but it does not bring much to the table that I can see. I personally will stick to C arrays over this thing, but a pureist / OOP/OSO minded programmer is going to prefer this. It fits better in OPP design patterns, but its a bit overkill if you just want to store a few values in a block.

And vector, which is really a wrapper for pointers more than a type of arrays, which comes from the C and older c++ ideas of allocating a block under a pointer and accessing those as an array more than anything else. Vectors are awesome, they support tons of useful functions. They resize for you, but this operation is very expensive and should be avoided as much as possible. They are probably the most used container and one of the most powerful and important items in modern c++.

I am actually not sure if std::array allocates on the heap or the stack. I suspect it is on the stack, in which case that is another difference as vectors allocate via pointers on the heap.

Last edited on
Very useful replies, thank you guys, really helped. :) Marked as solved.
Topic archived. No new replies allowed.