Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between std::valarray and std::array

valarray class look's same to array class, can you please explain me where would I prefer valarray over array or vice versa?

like image 534
codekiddy Avatar asked Jan 22 '12 23:01

codekiddy


People also ask

What is the difference between std :: array and std :: vector?

Difference between std::vector and std::array in C++Vector is a sequential container to store elements and not index based. Array stores a fixed-size sequential collection of elements of the same type and it is index based. Vector is dynamic in nature so, size increases with insertion of elements.

What is an STD Valarray?

std::valarray is the class for representing and manipulating arrays of values. It supports element-wise mathematical operations and various forms of generalized subscript operators, slicing and indirect access.

Which is better array or Vector?

Vector is better for frequent insertion and deletion, whereas Arrays are much better suited for frequent access of elements scenario. Vector occupies much more memory in exchange for managing storage and growing dynamically, whereas Arrays are a memory-efficient data structure.

What's the difference between array and Vector?

An array is a data structure that stores a fixed number of elements (elements should of the same type) in sequential order. A Vector is a sequential-based container. Arrays can be implemented in a static or dynamic way. Vectors can only be implemented dynamically.


2 Answers

  • valarray was already in C++03, array is new in C++11
  • valarray is variable length, array is not.
  • valarray is designed for numeric computations and provides plenty of operations including +, -, *, cos, sin, etc... array does not.
  • valarray has an interface to retrieve slices of the array (sub arrays), array does not.
like image 163
Yakov Galka Avatar answered Sep 23 '22 07:09

Yakov Galka


valarray is a dynamic data structure, whose size can change at runtime and which performs dynamic allocation. array is a static data structure whose size is determined at compile time (and it is also an aggregate).

Don't use valarray, though; just use a vector instead.

like image 25
Kerrek SB Avatar answered Sep 25 '22 07:09

Kerrek SB