Array Type Manipulation in C++ Last Updated : 28 May, 2017 Comments Improve Suggest changes Like Article 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 Comment More infoAdvertise with us Next Article Array Type Manipulation in C++ K kartik Follow Improve Article Tags : C Language C++ CPP-Library cpp-array CPP Array and String +1 More Practice Tags : CPP Similar Reads array::operator[ ] in C++ STL Array classes are generally more efficient, light-weight, and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::operator[] This operator is used to reference the element present at position given inside the operator. 2 min read How to Take Input in Array in C++? Arrays in C++ are derived data types that can contain multiple elements of the same data type. They are generally used when we want to store multiple elements of a particular data type under the same name. We can access different array elements using their index as they are stored sequentially in th 3 min read Pointers vs Array in C++ Arrays and pointers are two derived data types in C++ that have a lot in common. In some cases, we can even use pointers in place of arrays. But even though they are so closely related, they are still different entities. In this article, we will study how the arrays and pointers are different from e 3 min read Pointer to an Array in C++ Pointers in C++ are variables that store the address of another variable while arrays are the data structure that stores the data in contiguous memory locations. In C++, we can manipulate arrays by using pointers to them. These kinds of pointers that point to the arrays are called array pointers or 6 min read One Dimensional Arrays in C++ One-dimensional arrays are like a row of boxes where you can store things where each box can hold one item, such as a number or a word. For example, in an array of numbers, the first box might hold 5, the second 10, and so on. You can easily find or change what's in each box by referring to its posi 6 min read Like