Should I use vector or just double array?

I am programming about some numerical problems. I know that vector supplies vector operations. But vector always allocate more memory (used when the size changes). My matrix or array never change size, and the vector operation is just +,-,dot,cross,distance

My question is that should I use vector, or simple double array with new & delete is enough for me?
vector supplies vector operations
What do you mean by vector operations?

vector always allocate more memory
Nope. Not always. And you can tell it to reserve specific amount. And even if it is, extra memory used is unnoticeable.

My question is that should I use vector, or simple double array with new & delete is enough for me?
If your array never changes sizes, a single static array would be fine (std::array even better). If not, a simple one-dimentional vector pretending to be two-dimensional (your class should handle that) would be perfect choice.
If you really do not want to use a vector, use array form of unique_ptr instead.
Note that the vector class in the standard library (std::vector) is just a container class meant to store elements. It does not have the vector math operations you mentioned.
Consider using std::valarray<>.
http://en.cppreference.com/w/cpp/numeric/valarray
¿is there something similar to valarray but that is pod?
What do you mean by vector operations?
Maybe I have made a mistake, there is no such math operations, as mentioned by Peter87.

Consider using std::valarray<>
Somebody said that std::valarray is an unsuccessful class, and it's not recommended to use it.
> Somebody said that std::valarray is an unsuccessful class, and it's not recommended to use it.

It became an orphaned class when vector processors went out of vogue and expression templates became mainstream. But all compiler writers have not been ignoring it completely.

Its performance is truly horrible with the Microsoft implementation; while both GNU and LLVM have got it to perform on par with c-style array/vector. And Intel has a high performance valarray implementation.

http://coliru.stacked-crooked.com/a/590cea47431a886f
https://software.intel.com/sites/products/documentation/doclib/iss/2013/compiler/cpp-lin/GUID-A2AEF527-422D-4B7C-A411-6B9CE4D0DBC7.htm

Even if valarray performs just on par with c-style array/vector, its interface becomes an attractive choice for computing with arrays of NumericTypes.
Last edited on
Topic archived. No new replies allowed.