本文演示了一些内置函数,这些函数可用于查询和操作数组类型,甚至是多维数组。这些函数在我们需要信息或操纵不同维度的数组时非常有用。这些函数在头文件中定义。其中一些功能包括:
null
- is_array(): 顾名思义,此函数的唯一目的是检查变量是否为数组类型。值得注意的是,即使是 std::数组 根据此函数,不被视为数组。如果type为array,则“value”成员常量返回true,否则返回false。
- 是一样的吗 此函数用于检查 类型关系 如果两种类型具有完全相同的特征,则返回true。如果类型相同,“value”成员常量返回true,否则返回false。
// C++ code to demonstrate the working of
// is_array() and is_same()
#include<type_traits>
#include<iostream>
#include<array>
#include<string>
using
namespace
std;
int
main()
{
// checking which is array using is_array
cout <<
"Is Integer an array? : "
<< is_array<
int
>::value << endl;
cout <<
"Is Array an array? : "
<< is_array<
int
[10]>::value << endl;
cout <<
"Is 2D Array an array? : "
<< is_array<
int
[10][10]>::value << endl;
cout <<
"Is String an array? : "
<< is_array<string>::value << endl;
cout <<
"Is Character Array an array? : "
<< is_array<
char
[10]>::value << endl;
cout <<
"Is Array class type an array? : "
<< is_array<array<
int
,3>>::value << endl;
cout << endl;
// checking for same types using is_same()
cout <<
"Is 2D array same as 1D array? : "
<<
is_same<
int
[10],
int
[10][10]>::value << endl;
cout <<
"Is Character array same as Integer array? : "
<< is_same<
int
[10],
char
[10]>::value << endl;
cout <<
"Is 1D array same as 1D array (Different sizes) ? : "
<< is_same<
int
[10],
int
[20]>::value << endl;
cout <<
"Is 1D array same as 1D array? (Same sizes): "
<< is_same<
int
[10],
int
[10]>::value << endl;
return
0;
}
输出:
Is Integer an array? : 0 Is Array an array? : 1 Is 2D Array an array? : 1 Is String an array? : 0 Is Character Array an array? : 1 Is Array class type an array? : 0 Is 2D array same as 1D array? : 0 Is Character array same as Integer array? : 0 Is 1D array same as 1D array (Different sizes) ? : 0 Is 1D array same as 1D array? (Same sizes): 1
- 秩(): 这是一个 属性查询函数 返回数组的秩。等级意味着 数组的维数 .值成员常量返回对象的秩。
// C++ code to demonstrate the working of
// rank()
#include<type_traits> // for array query functions
#include<iostream>
using
namespace
std;
int
main()
{
// checking rank of different types
cout <<
"The rank of integer is : "
<< rank<
int
>::value << endl;
cout <<
"The rank of 1D integer array is : "
<< rank<
int
[10]>::value << endl;
cout <<
"The rank of 2D integer array is : "
<< rank<
int
[20][10]>::value << endl;
cout <<
"The rank of 3D integer array is : "
<< rank<
int
[20][10][40]>::value << endl;
cout <<
"The rank of 1D character array is : "
<< rank<
char
[10]>::value << endl;
cout << endl;
}
输出:
The rank of integer is : 0 The rank of 1D integer array is : 1 The rank of 2D integer array is : 2 The rank of 3D integer array is : 3 The rank of 1D character array is : 1
- 范围(): “范围”和“删除范围”都是 复合型蚀变 可以应用于C++中的数组。此函数返回数组的特定维度的大小。此函数接受两个参数:数组类型和必须找到其大小的维度。这也有用于打印值的成员常量值。
- 删除_extent(): 此函数用于删除所声明矩阵/数组中左侧的第一个维度。
- 删除所有扩展数据块(): 此函数用于删除矩阵/数组的所有维度,并将其转换为基本数据类型。
// C++ code to demonstrate the working of
// extent(), remove_extent(), remove_all_extents()
#include<type_traits> // for array query functions
#include<iostream>
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<
int
[20][10][40],0>::value << endl;
cout <<
"The extent of 2nd dimension of 3D integer array is : "
;
cout << extent<
int
[20][10][40],1>::value << endl;
cout <<
"The extent of 3rd dimension of 3D integer array is : "
;
cout << extent<
int
[20][10][40],2>::value << endl;
cout <<
"The extent of 4th dimension of 3D integer array is : "
;
cout << extent<
int
[20][10][40],3>::value << endl;
cout << endl;
// Removing extent of types
cout <<
"The rank after removing 1 extent is : "
;
cout << rank<remove_extent<
int
[20][10][30]>::type>::value << endl;
// 1st dimension from left is deleted
cout <<
"The extent of 1st after removing 1 extent is : "
;
cout << extent<remove_extent<
int
[20][10][30]>::type>::value << endl;
cout << endl;
// Removing all extents of types
cout <<
"The rank after removing all extents is : "
;
cout << rank<remove_all_extents<
int
[20][10][30]>::type>::value << endl;
// All extents are deleted
cout <<
"The extent of 1st after removing all extents is : "
;
cout << extent<remove_all_extents<
int
[20][10][30]>::type>::value << endl;
cout << endl;
}
输出:
The extent of 1st dimension of 3D integer array is : 20 The extent of 2nd dimension of 3D integer array is : 10 The extent of 3rd dimension of 3D integer array is : 40 The extent of 4th dimension of 3D integer array is : 0 The rank after removing 1 extent is : 2 The extent of 1st after removing 1 extent is : 10 The rank after removing all extents is : 0 The extent of 1st after removing all extents is : 0
如果你喜欢Geeksforgek,并想贡献自己的力量,你也可以写一篇文章,然后把你的文章发到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END