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
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP