C++数组类型操作


数组是C++中一种数据结构,用于在连续的内存位置存储多个相同数据类型的元素。

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

一些在C++中操作数组的常用方法:

is_array()

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

示例

 在线演示

#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

is_same()

此函数用于检查传递的两种类型是否具有完全相同的蓝图,即两种类型的类型应该相同。让我们来看这个例子,它将阐明这个概念:

示例

 在线演示

#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()

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++中的extent()方法返回数组某一维的大小。此方法有两个输入参数:数组和维数。

示例

 在线演示

#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 << "length of 1st dimension after removal is :";
   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
length of 1st dimension after removal is :10

remove_all_extents()

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

示例

 在线演示

#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 << "length of 1st dimension after removal is :";
   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
length of 1st dimension after removal is :0

更新于:2019年10月24日

2K+ 浏览量

启动您的职业生涯

通过完成课程获得认证

开始学习
广告