Array Type Manipulation in C++ Last Updated : 28 May, 2017 Comments Improve Suggest changes 10 Likes Like Report This article demonstrates some of the inbuilt functions that can be used to query and manipulate array types, even a multidimensional array. These functions can be useful in cases we need information or manipulate array we initiated with different dimensions. These functions are defined in header file . Some of the functions include : is_array() : As the name suggest, the sole purpose of this function is to check if a variable is a array type or not. Notable here is that even an std::array is not considered an array according to this function. The "value" member constant returns true if type is array, else returns a false. is_same() : This function is to check Type relationships and it returns true if two types have exactly same characteristics. The "value" member constant returns true if types are same, else returns a false. CPP // C++ code to demonstrate the working of // is_array() and is_same() #include<type_traits> #include<iostream> #include<array> #include<string> using namespace std; int main() { // checking which is array using 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; // checking for same types using 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; } Output: 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 rank() : This is a property query function which returns the rank of the array. Rank means the dimension of the array. The value member constant returns the rank of object. CPP // C++ code to demonstrate the working of // rank() #include<type_traits> // for array query functions #include<iostream> using namespace std; int main() { // checking rank of different types 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; } Output: 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 extent() : Both extent and remove extent are Compound type alterations that can be applied to arrays in C++. This function returns the size of the particular dimension of array. This function takes two arguments, the array type and the dimension in whose size have to be found. This also has the member constant value for printing value. remove_extent() : This function removes the first dimension from left in the matrix/array declared. remove_all_extents() : This function removes all the dimensions of the matrix/array and converts it into base data type. CPP // C++ code to demonstrate the working of // extent(), remove_extent(), remove_all_extents() #include<type_traits> // for array query functions #include<iostream> using namespace std; int main() { // Checking extent of different types (using extent) 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; // Removing extent of types cout << "The rank after removing 1 extent is : " ; cout << rank<remove_extent<int[20][10][30]>::type>::value << endl; // 1st dimension from left is deleted cout << "The extent of 1st after removing 1 extent is : " ; cout << extent<remove_extent<int[20][10][30]>::type>::value << endl; cout << endl; // Removing all extents of types cout << "The rank after removing all extents is : " ; cout << rank<remove_all_extents<int[20][10][30]>::type>::value << endl; // All extents are deleted cout << "The extent of 1st after removing all extents is : " ; cout << extent<remove_all_extents<int[20][10][30]>::type>::value << endl; cout << endl; } Output: 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 Create Quiz Comment K kartik Follow 10 Improve K kartik Follow 10 Improve Article Tags : C++ CPP-Library cpp-array CPP Array and String Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like