rank() in c++
std::rank
in C++ is a template meta-function that retrieves the number of dimensions of an array type. It's a part of the <type_traits>
header in the C++ Standard Library.
Here's an example:
#include <iostream>
#include <type_traits>
int main() {
using ArrayType = int[5][10][15];
std::cout << "Rank of ArrayType: " << std::rank<ArrayType>::value << std::endl;
return 0;
}
Explanation:
#include <iostream>
and#include <type_traits>
: These lines include necessary headers for input/output and type traits functionalities respectively.using ArrayType = int[5][10][15];
: This line defines a type aliasArrayType
for a 3D array of integers with dimensions 5, 10, and 15.std::cout << "Rank of ArrayType: " << std::rank<ArrayType>::value << std::endl;
: This line usesstd::rank
to determine and output the number of dimensions (rank) of theArrayType
. It usesstd::rank<ArrayType>::value
to retrieve the rank as a compile-time constant and prints it to the console.return 0;
: The main function returns 0 to indicate successful completion.
The output of this program will be:
Rank of ArrayType: 3
This indicates that the ArrayType
is a 3-dimensional array.