C ++中的数组类型操作

数组是c ++中的数据结构,该结构在连续的内存位置中存储了相同数据类型的多个数据元素。

在c ++编程语言中,有内置的函数可以操纵数组类型。一些函数也可以应用于多维数组。数组头文件包含使用c ++编程语言处理数组的函数。

一些在c ++中操作数组的常用方法是-

is_array()

此函数用于检查传递给该函数的变量是否为数组类型。此方法在识别甚至std ::数组在检查中被拒绝的数组时非常严格。返回类型是整数,即如果传递数组则返回true(1),否则返回False。

示例

#include<type_traits>
#include<iostream>
#include<array>
#include<string>
using namespace std;
int main(){
   cout<<"Checking if int is an array ? : ";
   is_array<int>::value?cout<<"True":cout<<"False";
   cout<<"\nChecking if int[] is an array? : ";
   is_array<int[6]>::value?cout<<"True":cout<<"False";
   cout<<"\nChecking if 2D Array is an array? : ";
   is_array<int[2][3]>::value?cout<<"True":cout<<"False";
   cout<<"\nChecking if String is an array? : ";
   is_array<string>::value?cout<<"True":cout<<"False";
   cout<<"\nChecking if Character Array is an array? : ";
   is_array<char[4]>::value?cout<<"True":cout<<"False";
   cout << endl;
   return 0;
}

输出结果

Checking if int is an array ? : False
Checking if int[] is an array? : True
Checking if 2D Array is an array? : True
Checking if String is an array? : False
Checking if Character Array is an array? : True

是同样的()

此函数用于检查所传递的两种类型是否具有完全相同的设计图,即两者的类型应相同。让我们看看这个例子,它将清除概念-

示例

#include<type_traits>
#include<iostream>
#include<array>
#include<string>
using namespace std;
int main(){
   cout << "Checking if 1D array is same as 1D array (Different sizes) ? : " ;
   is_same<int[3],int[4]>::value?cout<<"True":cout<<"False";
   cout << "\nChecking if 1D array is same as 1D array? (Same sizes): " ;
   is_same<int[5],int[5]>::value?cout<<"True":cout<<"False";
   cout << "\nChecking If 2D array is same as 1D array? : ";
   is_same<int[3],int[3][4]>::value?cout<<"True":cout<<"False";
   cout << "\nChecking if Character array is same as Integer array? : " ;
   is_same<int[5],char[5]>::value?cout<<"True":cout<<"False";
   return 0;
}

输出结果

Checking if 1D array is same as 1D array (Different sizes) ? : False
Checking if 1D array is same as 1D array? (Same sizes): True
Checking If 2D array is same as 1D array? : False
Checking if Character array is same as Integer array? : False

秩()

rank函数用于返回传递的数组的等级。等级是指数组的维数。它返回数组等级的整数值。

示例

#include<type_traits>
#include<iostream>
using namespace std;
int main(){
   cout<<"Print rank for the following types of arrays : \n";
   cout<<"integer : "<<rank<int>::value<<endl;
   cout<<"1D integer array (int[]) : "<< rank<int[5]>::value<<endl;
   cout<<"2D integer array (int[][]) : "<<rank<int[2][2]>::value<<endl;
   cout<<"3D integer array (int[][][]) : "<<rank<int[2][3][4]>::value<<endl;
   cout<<"1D character array : "<<rank<char[10]>::value<<endl;
}

输出结果

Print rank for the following types of arrays :
integer : 0
1D integer array (int[]) : 1
2D integer array (int[][]) : 2
3D integer array (int[][][]) : 3
1D character array : 1

程度()

extent()c ++中的方法返回数组维的大小。此方法有两个输入参数:数组和维度。

示例

#include<type_traits>
#include<iostream>
using namespace std;
int main(){
   cout<<"Printing the length of all dimensions of the array arr[2][45][5] :\n";
   cout<<"1st dimension : "<<extent<int[2][45][5],0>::value<<endl;
   cout<<"2nd dimension : "<<extent<int[2][45][5],1>::value<<endl;
   cout<<"3rd dimension : "<<extent<int[2][45][5],2>::value<<endl;
   cout<<"4th dimension : "<<extent<int[2][45][5],3>::value<<endl;
}

输出结果

Printing the length of all dimensions of the array arr[2][45][5] :
1st dimension : 2
2nd dimension : 45
3rd dimension : 5
4th dimension : 0

remove_extent()

remove_extent函数用于删除多维数组的维。它删除数组的第一维。

示例

#include<type_traits>
#include<iostream>
using namespace std;
int main(){
   cout<<"Removing extent of the array arr[2][5][4] : \n";
   cout<<"Initial rank : "<<rank<int[2][5][4]>::value<<endl;
   cout<<"The rank after removing 1 extent is : " ;
   cout << rank<remove_extent<int[20][10][30]>::type>::value << endl;
   cout << "移除后第一维的长度为:";
   cout<<extent<remove_extent<int[20][10][30]>::type>::value << endl;
}

输出结果

Removing extent of the array arr[2][5][4] :
Initial rank : 3
The rank after removing 1 extent is : 2
移除后第一维的长度为:10

remove_all_extents()

此函数用于一次性删除数组的所有维。数组更改为与array相同的变量。

示例

#include<type_traits>
#include<iostream>
using namespace std;
int main(){
   cout<<"Removing all extents of the array arr[2][5][4] : \n";
   cout<<"Initial rank : "<<rank<int[2][5][4]>::value<<endl;
   cout<<"The rank after removing all extents is : " ;
   cout << rank<remove_all_extents<int[20][10][30]>::type>::value << endl;
   cout << "移除后第一维的长度为:";
   cout<<extent<remove_all_extents<int[20][10][30]>::type>::value << endl;
}

输出结果

Removing all extents of the array arr[2][5][4] :
Initial rank : 3
The rank after removing all extents is : 0
移除后第一维的长度为:0