Open In App

Array Type Manipulation in C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
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 :

  1. 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.
  2. 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.




    // 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
    
  3. 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.




    // 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
    
  4. 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.
  5. remove_extent() : This function removes the first dimension from left in the matrix/array declared.
  6. remove_all_extents() : This function removes all the dimensions of the matrix/array and converts it into base data type.




    // 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
    


Last Updated : 28 May, 2017
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads