本文演示了一些内置函数,这些函数可用于查询和操作数组类型,甚至多维数组。 这些功能在我们需要信息或操纵以不同维度发起的数组的情况下很有用。这些函数在头文件中定义。其中一些功能包括:
1.is_array():顾名思义,此函数的唯一目的是检查变量是否为数组类型。 这里值得注意的是,根据此函数,即使std :: array也不会被视为数组。如果类型为数组,则“值”成员常量将返回true,否则返回false。
2.is_same():此函数用于检查类型关系,如果两个类型具有完全相同的特征,则返回true。如果类型相同,则“value”成员常量返回true,否则返回false。
// C++ code to demonstrate the working of // is_array() and is_same() #include #include #include #include using namespace std; int main() { // checking which is array using is_array cout << "Is Integer an array? : " << is_array::value << endl; cout << "Is Array an array? : " << is_array::value << endl; cout << "Is 2D Array an array? : " << is_array::value << endl; cout << "Is String an array? : " << is_array::value << endl; cout << "Is Character Array an array? : " << is_array::value << endl; cout << "Is Array class type an array? : " << is_array>::value << endl; cout << endl; // checking for same types using is_same() cout << "Is 2D array same as 1D array? : " << is_same::value << endl; cout << "Is Character array same as Integer array? : " << is_same::value << endl; cout << "Is 1D array same as 1D array (Different sizes) ? : " << is_same::value << endl; cout << "Is 1D array same as 1D array? (Same sizes): " << is_same::value << endl; return 0; }
输出:
Is Integer an array? : 0Is Array an array? : 1Is 2D Array an array? : 1Is String an array? : 0Is Character Array an array? : 1Is Array class type an array? : 0Is 2D array same as 1D array? : 0Is Character array same as Integer array? : 0Is 1D array same as 1D array (Different sizes) ? : 0Is 1D array same as 1D array? (Same sizes): 1
3.rank():这是一个属性查询函数,它返回数组的秩。Rank表示数组的维数。值成员常量返回对象的秩。
// C++ code to demonstrate the working of // rank() #include // for array query functions #include using namespace std; int main() { // checking rank of different types cout << "The rank of integer is : " << rank::value << endl; cout << "The rank of 1D integer array is : " << rank::value << endl; cout << "The rank of 2D integer array is : " << rank::value << endl; cout << "The rank of 3D integer array is : " << rank::value << endl; cout << "The rank of 1D character array is : " << rank::value << endl; cout << endl; }
输出:
he rank of integer is : 0The rank of 1D integer array is : 1The rank of 2D integer array is : 2The rank of 3D integer array is : 3The rank of 1D character array is : 1
4.extent() :两个范围和删除范围都是复合类型的改变,可以应用于C++中的数组。此函数返回数组的特定维度的大小。此函数接受两个参数,即数组类型和必须找到其大小的维。 这也具有用于打印值的成员常数值。
5.remove_extent():此函数从声明的矩阵/数组的左侧开始删除第一维。
6.remove_all_extents():此函数删除矩阵/数组的所有维并将其转换为基本数据类型。
// C++ code to demonstrate the working of // extent(), remove_extent(), remove_all_extents() #include // for array query functions #include using namespace std; int main() { // Checking extent of different types (using extent) cout << "The extent of 1st dimension of 3D integer array is : " ; cout << extent::value << endl; cout << "The extent of 2nd dimension of 3D integer array is : " ; cout << extent::value << endl; cout << "The extent of 3rd dimension of 3D integer array is : " ; cout << extent::value << endl; cout << "The extent of 4th dimension of 3D integer array is : " ; cout << extent::value << endl; cout << endl; // Removing extent of types cout << "The rank after removing 1 extent is : " ; cout << rank::type>::value << endl; // 1st dimension from left is deleted cout << "The extent of 1st after removing 1 extent is : " ; cout << extent::type>::value << endl; cout << endl; // Removing all extents of types cout << "The rank after removing all extents is : " ; cout << rank::type>::value << endl; // All extents are deleted cout << "The extent of 1st after removing all extents is : " ; cout << extent::type>::value << endl; cout << endl; }
输出:
The extent of 1st dimension of 3D integer array is : 20The extent of 2nd dimension of 3D integer array is : 10The extent of 3rd dimension of 3D integer array is : 40The extent of 4th dimension of 3D integer array is : 0The rank after removing 1 extent is : 2The extent of 1st after removing 1 extent is : 10The rank after removing all extents is : 0The extent of 1st after removing all extents is : 0