×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

std::valarray class in C++

C++ STL | std::valarray Class: Learn about the valarray class, its features, and its methods with examples. By Shivang Yadav Last updated : December 11, 2023

C++ std::valarray class

The valarray class in C++ is a special container for holding an array. The valarray object also provides mathematical operations on the array.

  • The operations like mathematical operations, slicing, and direct access can be done to all elements individually in the valarray.
  • This valarray is also able to perform mathematical operations as effectively as vectors.
  • The valarray class was introduced in C++ 98.

Declaration Syntax

The valarray needs the valarray library to be included using the following syntax:

#include<valarray>

The syntax to declare valarray:

valarray <type> valarrayName;

Program to declare valarray in C++

#include <iostream>
#include <valarray>
using namespace std;

int main()
{
    // Declaring valarray
    valarray<int> myvalarr = { 5, 7, 2, 8, 1, 9 };

    // Printing the elements of valarray
    cout << "The elements stored in valarray are : ";
    for (int& ele : myvalarr)
        cout << ele << " ";
    return 0;
}

Output:

The elements stored in valarray are : 5 7 2 8 1 9

C++ valarray Class Functions

Function Description
std::valarray::apply() It is used to apply a manipulation operation to all the elements of the valarray.
std::begin (valarray) It is used to return an iterator which points to the element at the first index position.
std::end (valarray) It is used to return an iterator which points to the element next to the element at the last index position.
std::valarray::max() and std::valarray::min() These are used to find the maximum and minimum values elements of the valarray.
std::sin(std::valarray) It is used to find the sin value of each element of the valarray.
std::valarray<T>::sum() It is used to simply find the sum of all elements of the valarray.
std::swap (valarray) It is used to swap valarray with one another.
std::tan(std::valarray) It is used to hold multiple values just like an array and performing operations on them.
std::log(std::valarray) It is used to calculate the value of the log of elements of the valarray. It returns a valarray with each element calculated using log() function.
std::log10(std::valarray) It is used to calculate the value of the log base 10 of elements of the valarray. It returns a valarray with each element calculated using log10() function.
std::abs(std::valarray) It is used to calculate the absolute value of elements of valarray. It returns a valarray with each element calculated using the abs() function.
std::exp(std::valarray) It is used to calculate the value of e raised to the value of element. It returns a valarray with each element calculated using exp() function.
std::pow(std::valarray) It is used to return a valarray that contains elements that are elements raised to the power N.
std::valarray::size() It is used to return the size of the valarray. The function counts the total number of elements present in the valarray and returns the count.
std::sqrt(std::valarray) It is used to hold multiple values just like an array and perform operations on them.
std::asin(std::valarray) It is a function used for a valarray consisting of elements calculated as sin-1 for each element of the original valarray.
std::acos(std::valarray) It is a function used for creating a valarray consisting of elements calculated as cos-1 for each element of the original valarray.
std::atan(std::valarray) It is a function used for creating a valarray consisting of elements calculated as tan-1 for each element of the original valarray.
valarray Class - Hyperbolic Functions

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.