Search code examples
c++arraysalgorithmmaxunsigned-integer

request for member 'append' in 'a' , which is of non class type 'int'


im new to programming so my doubts are pretty sily maybe. so when im trying to solve a challenge in which i have to form the biggest number from the digits in array. so i created a compare function and passes in to the sort function. in compare function im check if a+b is bigger than b+a and vice versa. but the compiler shows the error stating request for member non class type.

i actually dont have any idea on how to solve this, i tried googling stuff but didnt got my solution. i found the similar solution but they are doing it with string. can it be done with array.

int myCompare( int a, int b){


    int ab= a.append(b);
    int ba= b.append(a);

    if(ab>ba){
        return 1;
    }else{
        return 0;
    }

}

void biggestnum(int arr[], int n){

    if(n==0 || n==1){
        return;
    }

    sort(arr,arr+n, myCompare);

    for(int i=0;i<n;i++){
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}

so if the given array is {54, 546, 548, 60}; the result should be 6054854654.


Solution

  • The scalar fundamental type int does not have methods. So these records

    a.append(b);
    b.append(a);
    

    are invalid.

    It seems using such a construction like this

    a.append(b)
    

    you are trying to convert an integer to an object of the class std::string. However if a and b were objects of the type std::string there is no sense to append one object to another. It is enough to compare the original strings.

    It seems what you are trying to achieve is the following

    #include <iostream>
    #include <string>
    #include <iterator>
    #include <algorithm>
    
    bool myCompare( unsigned int a, unsigned int b )
    {
        std::string s1 = std::to_string( a );
        std::string s2 = std::to_string( b );
    
        auto n = std::minmax( { s1.size(), s2.size() } );
    
        int result = s1.compare( 0, n.first, s2, 0, n.first );
    
        return ( result > 0 ) || 
               ( result == 0 && 
               ( ( s1.size() < n.second && s2[n.first] < s1[0] ) || 
                 ( s2.size() < n.second && s2[0] < s1[n.first] ) ) );
    }    
    
    int main() 
    {
        unsigned int a[] = { 54, 546, 548, 60 };
    
        std::sort( std::begin( a ), std::end( a ), myCompare );
    
        std::string s;
    
        for ( const auto &item : a )
        {
            s += std::to_string( item );
        }
    
        unsigned long long int value = std::stoull( s );
    
        std::cout << "The maximum number is " << value << '\n';
    
        return 0;
    }
    

    The program output is

    The maximum number is 6054854654
    

    if the result number can be too big to fit into an object of the type unsigned long long int then you can remove the statement

        unsigned long long int value = std::stoull( s );
    

    and just output the result string as

        std::cout << "The maximum number is " << s << '\n';