Reorder Data in Log Files(Leetcode)

Viewed 152
class Solution
{
public:
    bool cmp(string str1, string str2)
    {
        int sp1, sp2;
        for (int i = 0; i < str1.length(); i++)
        {
            if (str1[i] == ' ')
            {
                sp1 = i;
                break;
            }
        }
        for (int i = 0; i < str2.length(); i++)
        {
            if (str2[i] == ' ')
            {
                sp2 = i;
                break;
            }
        }
        string s1 = "", s2 = "";
        for (int i = sp1 + 1; i < str1.length(); i++)
            s1 += str1[i];
        for (int i = sp2 + 1; i < str2.length(); i++)
            s2 += str2[i];
        return s1 <= s2;
    }
    vector<string> reorderLogFiles(vector<string> &logs)
    {
        int n = logs.size();
        vector<string> ansLog(n);
        int start = 0, end = n - 1;

        for (auto str : logs)
        {
            reverse(str.begin(), str.end());
            if (str[0] >= 48 && str[0] <= 57)
            {
                reverse(str.begin(), str.end());
                ansLog[end] = str;
                end--;
            }
            else
            {
                reverse(str.begin(), str.end());
                ansLog[start] = str;
                start++;
            }
        }
        
        end++;
        reverse(ansLog.begin() + end, ansLog.end());
        sort(ansLog.begin(), ansLog.begin() + start, cmp);
        return ansLog;
    }
};

Can anyone help me why i am getting error in sort(ansLog.begin(), ansLog.begin() + start, cmp); . I am using comparator to sort the vector from begining to index start-1.
Question Link: https://leetcode.com/problems/reorder-data-in-log-files/

1 Answers

Not focusing on the solution of the problem but only focusing on the error. It is more helpful if you post your error as well. But hoping you error was like below:

error: reference to non-static member function must be called
        sort(ansLog.begin(), ansLog.begin() + start, cmp);
                                                     ^~~
1 error generated.

The issue here is the cmp method is here is non-static class member which you'll need to bind this with this and two arguments that it takes. There can be multiple solutions for this:

  1. You can consider using boost::function to bind this:
sort(ansLog.begin(), ansLog.begin() + start,  boost::bind( &Solution::cmp, this, _1, _2 ));
  1. Since this function is not using any class property, you can simply make it static and use it the way you are using.
  2. The better way would be I believe is use a functor like below:
    class Comparator {
        public:
        bool operator()(string str1, string str2) {
            int sp1, sp2;
            for (int i = 0; i < str1.length(); i++)
            {
                if (str1[i] == ' ')
                {
                    sp1 = i;
                    break;
                }
            }
            for (int i = 0; i < str2.length(); i++)
            {
                if (str2[i] == ' ')
                {
                    sp2 = i;
                    break;
                }
            }
            string s1 = "", s2 = "";
            for (int i = sp1 + 1; i < str1.length(); i++)
                s1 += str1[i];
            for (int i = sp2 + 1; i < str2.length(); i++)
                s2 += str2[i];
            return s1 <= s2;
        }
    };

And use it as:

sort(ansLog.begin(), ansLog.begin() + start, Comparator());
Related