C++中的数组类型操作

简介: 本文演示了一些可用于查询和操作数组类型(甚至是多维数组)的内置函数。在我们需要信息或操作我们用不同维度启动的数组的情况下,这些函数非常有用。这些函数在头文件 中定义。一些功能包括:

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第2天,点击查看活动详情


本文演示了一些可用于查询和操作数组类型(甚至是多维数组)的内置函数。在我们需要信息或操作我们用不同维度启动的数组的情况下,这些函数非常有用。这些函数在头文件 中定义。一些功能包括:


  1. is_array() : 顾名思义,此函数的唯一目的是检查变量是否为数组类型。这里值得注意的是,根据此函数,即使是 std::array 也不被视为数组。如果类型是数组,则“value”成员常量返回true,否则返回false。

  2. is_same(): 此函数用于检查类型关系,如果两个类型具有完全相同的特征,则返回 true。如果类型相同,则“value”成员常量返回 true,否则返回 false。
// 演示is_array()和is_same()工作的C++代码
#include<type_traits>
#include<iostream>
#include<array>
#include<string>
using namespace std;
int main()
{
  // 使用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;
  // 使用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;
}
复制代码

  1. 输出:
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
复制代码

  1. 等级() : 这是一个属性查询函数,它返回数组的秩。秩表示数组的维度。值成员常量返回对象的秩。

// 演示rank()工作的C++代码
#include<type_traits> // 用于数组查询函数
#include<iostream>
using namespace std;
int main()
{
  // 检查不同类型的等级
  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
复制代码

  1. extent(): 范围和移除范围都是复合类型更改,可应用于C++中的数组。此函数返
  2. 回数组特定维度的大小。此函数接受两个参数,数组类型和必须找到其大小的维度。这也具有打印值的成员常量值。

  3. remove_extent() : 此函数删除声明的矩阵/数组中左侧的第一个维度。

  4. remove_all_extents(): 此函数删除矩阵/数组的所有维度并将其转换为基本数据类型。


// C++代码演示extent()、remove_extentt()和remove_all_extents()的工作
#include<type_traits> // 用于数组查询函数
#include<iostream>
using namespace std;
int main()
{ 
  // 检查不同类型的范围(使用范围)
  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;
  // 正在删除类型的范围
  cout << "The rank after removing 1 extent is : " ;
  cout << rank<remove_extent<int[20][10][30]>::type>::value << endl;
  //删除左侧的第一个维度
  cout << "The extent of 1st after removing 1 extent is : " ;
  cout << extent<remove_extent<int[20][10][30]>::type>::value << endl;
  cout << endl;
  // 删除类型的所有范围
  cout << "The rank after removing all extents is : " ;
  cout << rank<remove_all_extents<int[20][10][30]>::type>::value << endl;
  // 删除所有范围
  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
复制代码


如果大家发现什么不正确的地方,或者你想分享有关上述数组类型操作的更多内容,可以在下面评论。



目录
相关文章
|
2天前
|
C++
C++ 数学函数、头文件及布尔类型详解
C++ 支持数学操作,如`max`和`min`函数找最大值和最小值,以及`&lt;cmath&gt;`库中的`sqrt`、`round`等数学函数。`bool`类型用于布尔逻辑,取值`true`(1)或`false`(0)。布尔表达式结合比较运算符常用于条件判断,例如在`if`语句中检查年龄是否达到投票年龄。在代码示例中,`isCodingFun`和`isFishTasty`变量分别输出1和0。
125 1
|
2天前
|
人工智能 机器人 C++
【C++/Python】Windows用Swig实现C++调用Python(史上最简单详细,80岁看了都会操作)
【C++/Python】Windows用Swig实现C++调用Python(史上最简单详细,80岁看了都会操作)
|
2天前
|
安全 编译器 程序员
【C++入门到精通】C++类型的转换 | static_cast | reinterpret_cast | const_cast | dynamic_cast [ C++入门 ]
【C++入门到精通】C++类型的转换 | static_cast | reinterpret_cast | const_cast | dynamic_cast [ C++入门 ]
14 0
|
2天前
|
存储 安全 算法
【C++入门到精通】 原子性操作库(atomic) C++11 [ C++入门 ]
【C++入门到精通】 原子性操作库(atomic) C++11 [ C++入门 ]
15 1
|
2天前
|
C++
【C++】istream类型对象转换为逻辑条件判断值
【C++】istream类型对象转换为逻辑条件判断值
【C++】istream类型对象转换为逻辑条件判断值
|
2天前
|
存储 C++
【C++模板】模板实现通用的数组
【C++模板】模板实现通用的数组
|
2天前
|
存储 人工智能 C++
【重学C++】【指针】详解让人迷茫的指针数组和数组指针
【重学C++】【指针】详解让人迷茫的指针数组和数组指针
34 1
|
2天前
|
C++
【C++】std::string 转换成非const类型 char* 的三种方法记录
【C++】std::string 转换成非const类型 char* 的三种方法记录
8 0
|
2天前
|
存储 C++
二叉树的操作(C++实现)
二叉树的操作(C++实现)
|
2天前
|
C++
有序链表的操作(底层c++实现)
有序链表的操作(底层c++实现)