
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Valarray::pow Function
The C++ Valarray::pow() function returns a valarray holding the results of the power operation on all the elements, in the same order. The calculations' outcomes are x raised to the power of y. (xy).
Every element in both x and y receives a single call to pow, which is utilized for all calls if either is a single T value. Cmath's pow is overloaded by this function.
Syntax
Following is the syntax for C++ Valarray::pow Function −
pow (const valarray<T>& x, const valarray<T>& y); pow (const valarray<T>& x, const T& y); pow (const T& x, const valarray<T>& y);
Parameters
x − It indicates the element with the bases for the power operations.
y − It indicates the element with the exponents for the power operations.
Examples
Example 1
Let's look into the following example, where we are going to use pow() function and retrieving the output.
#include <iostream> #include <valarray> using namespace std; int main() { valarray<int> varr = { -2,0,-3,4,-5 }; valarray<int> valarray1; valarray1 = pow(varr, 5); cout << "The New pow" << " Valarray is : " << endl; for (int& a : valarray1) { cout << a << " "; } cout << endl; return 0; }
Output
Let us compile and run the above program, this will produce the following result −
The New pow Valarray is : -32 0 -243 1024 -3125
Example 2
Let's consider the following example, where we are going to use pow() function and retrieving output of both original valarray and pow valarray.
#include <iostream> #include <valarray> using namespace std; int main() { valarray<double> myvalarray = { 2,4,0.6,-8,-9 }; cout << "The Orignal Valarray is : "; for (double& ele : myvalarray) cout << ele << " "; valarray<double> powValarray; powValarray = pow(myvalarray, 2); cout << "\nThe pow Valarray is : "; for (double& ele : powValarray) cout << ele << " "; return 0; }
Output
Let us compile and run the above program, this will produce the following result −
The Orignal Valarray is : 2 4 0.6 -8 -9 The pow Valarray is : 16 0.36 64 81
Example 3
In the following example, we are going to use pow() function with (const std::valarray<t>& base, const std::valarray<t>& exp) and retrieving the output.
#include <cmath> #include <cstddef> #include <iomanip> #include <iostream> #include <valarray> class tutorial { friend std::ostream& operator<< (std::ostream& os, tutorial const& r) { constexpr char const* sup[] { "\u2070", "\u00B9", "\u00B2", "\u00B3", "\u2074", "\u2075", "\u2076", "\u2077", "\u2078", "\u2079", }; for (std::size_t n = 0; n != r.base.size(); ++n) { os << std::left << r.base[n] << std::left; if (n < r.exponent.size()) os << sup[r.exponent[n] % 10] << " "; else os << " "; } if (r.results.size() != 0) { os << "="; for (std::size_t n = 0; n != r.results.size(); ++n) os << " " << r.results[n]; } return os << '\n'; } public: std::valarray<int> base{}, exponent{}, results{}; }; int main() { const std::valarray<int> base { 2,4,6,8,10}; const std::valarray<int> exponent {1,3,5,7,9 }; const std::valarray<int> power1 = std::pow(base, exponent); std::cout<< "pow : " << tutorial{base, exponent, power1}; }
Output
Let us compile and run the above program, this will produce the following result −
pow : 2 4 6 8 10 = 2 64 7776 2097152 1000000000