
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Array Type Manipulation in C++
The array is a data structure in c++ that stored multiple data elements of the same data type in continuous memory locations.
In c++ programming language, there are inbuilt functions to manipulate array types. Some functions can also be applied to multidimensional arrays. The array header file contains functions to manipulate arrays in c++ programming language.
Some common methods to manipulate arrays in c++ are −
is_array()
This function is used to check if the variable passed to the function is of the type array or not. This method is strict in recognizing arrays that even std:: array is rejected in the check. The return type is an integer i.e. it returns true(1) if an array is passed otherwise False (0).
Example
#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; }
Output
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()
This function is used to check if the two types passed have exact same blueprints i.e. the types of both should be the same. Let’s see this example which will clear the concept −
Example
#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; }
Output
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()
The rank function is used to return the rank of the array passed. Rank means the dimension of the array. It returns an integer value of the rank of the array.
Example
#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; }
Output
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()
The extent() method in c++ returns the size of a dimension of an array. There are two input parameters for this method, array, and the dimension.
Example
#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; }
Output
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()
The remove_extent function is used to remove the dimension of a multidimensional array. It deletes the first dimension of the array.
Example
#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; }
Output
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()
This function is used to remove all the dimensions of the array in one go. The array is changed to a variable of the same as of array.
Example
#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; }
Output
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