template <typename T> void print(const valarray<T> &va){
for(size_t i = 0; i < va.size(); i++)
cout << va[i] << ' ';
cout << endl;
}
Constructors
Default Constructor
valarray();
Creates an empty valarray instance.
Single Element Constructor
valarray(value, count);
Constructs a valarray with 'count' elements, each initialized to 'value'. Example:
valarray<int> numbers(7, 8);
print(numbers);
Output:
7 7 7 7 7 7 7 7
Container-Based Constructor
valarray(first, count);
Creates a valarray by taking 'count' elements starting from 'first' in an array or similar container. Example:
int dataset[] = {3, 1, 4, 1, 5, 9, 2, 6};
valarray<int> fullArray(dataset, sizeof(dataset) / sizeof(int));
valarray<int> partialArray(dataset + 3, 5);
print(fullArray);
print(partialArray);
Output:
3 1 4 1 5 9 2 6
1 5 9 2 6
Caution: Ensure the array size is sufficient. Accessing beyond array bounds may cause undefined behavior:
int data[5] = {10, 20, 30, 40, 50};
valarray<int> oversized(data, 10); // Potential undefined behavior
Nested Construction
Construct one valarray fromm another, possibly using slices:
int source[] = {5, 10, 15, 20, 25, 30};
valarray<int> baseArray(source, 6);
valarray<int> copiedArray(baseArray);
valarray<int> slicedArray(baseArray[slice(1, 3, 2)]);
print(copiedArray);
print(slicedArray);
Output:
5 10 15 20 25 30
10 20 30
The slice constructor parameters are: slice(start, length, stride), which selects 'length' elements starting at 'start', with a stride of 'stride' between elements.
Destructor
~valarray();
Invokes the destructor for each element, with linear time complexity relative to the array size. Local valarray instances automatically call the destructor when they go out of scope.
Operator Overloads
valarray overloads most common operators (except ++ and --), applying them element-wise. Example:
int values[] = {2, 4, 6, 8, 10};
valarray<int> arr1(values, 5);
valarray<int> arr2(3, 5); // 3 3 3 3 3
arr2 += arr1; // 5 7 9 11 13
arr2 -= 2; // 3 5 7 9 11
arr1 -= arr2; // -1 -1 -1 -1 -1
arr1 *= 1000; // -1000 -1000 -1000 -1000 -1000
arr2 *= arr1; // -3000 -5000 -7000 -9000 -11000
valarray<bool> comparison = (arr1 == arr2);
if(comparison.min()) cout << "Equal";
arr1 = 5; // 5 5 5 5 5
When performing operations between two valarrays of different lengths, the operation stops at the end of the shorter array. For comparison operators, the result is a valarray<bool> with length equal to the shorter input array.
Function Application
valarray.apply(function);
Applies a function to each element in the valarray. Example:
int halve(int x) { return x / 2; }
int main() {
valarray<int> data = {20, 40, 60, 80, 100};
valarray<int> result = data.apply(halve);
print(result);
return 0;
}
Output:
10 20 30 40 50
Mathematical Functions
valarray.max(); // Maximum value
valarray.min(); // Minimum value
valarray.sum(); // Sum of all elements
These functions perform the corresponding mathematical operations on all elements in the valarray.
Movement Functions
Circular Shift
valarray.cshift(count);
Shifts elements circularly by 'count' positions. Positive values shift left, negative values shift right. Example:
valarray<int> sequence = {1, 2, 3, 4, 5, 6, 7, 8, 9};
sequence = sequence.cshift(3);
print(sequence);
Output:
4 5 6 7 8 9 1 2 3
Linear Shift
valarray.shift(count);
Shifts elements linearly by 'count' positions, filling new positions with zero. Positive values shift left, negative values shift right. Example:
valarray<int> sequence = {1, 2, 3, 4, 5, 6, 7, 8, 9};
sequence = sequence.shift(2);
print(sequence);
Output:
3 4 5 6 7 8 9 0 0
For cshift, if |count| exceeds the array length, it's equivalent to count % length. For shift, if |count| exceeds the array length, all elements become zero.
Size Functions
Get Size
valarray.size();
Returns the number of elements in the valarray.
Resize
valarray.resize(new_size);
Changes the valarray size to 'new_size', setting all elements to zero. Example:
valarray<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
numbers.resize(4);
print(numbers);
Output:
0 0 0 0
Swap Function
valarray1.swap(valarray2);
Exchanges the contents of two valarrays. This is more efficeint than using the std::swap function:
valarray<double> a = {1.1, 2.2, 3.3};
valarray<double> b = {4.4, 5.5, 6.6};
a.swap(b);
// Now a contains {4.4, 5.5, 6.6} and b contains {1.1, 2.2, 3.3}