c – Two code one is giving error where as other doesn’t

Why i am getting error

terminate called after throwing an instance of 'std::length_error'
  what():  basic_string::_M_create 

on using this code

bool comp(string a,string b){
    // if(a==b) return false;
    if(a.size()>b.size()) return true;
    else if(a.size()<b.size()) return false;
    else{
        if(a<b) return false;
        else return true;
    }
}

class Solution {
public:
    string kthLargestNumber(vector<string>& nums, int k) {
        nth_element(nums.begin(),nums.begin()+k-1,nums.end(),comp);
        return nums(k-1);
    }
};

whereas this code shows no error

bool comp(string a,string b){
    if(a==b) return false;
    if(a.size()>b.size()) return true;
    else if(a.size()<b.size()) return false;
    else{
        if(a<b) return false;
        else return true;
    }
}

class Solution {
public:
    string kthLargestNumber(vector<string>& nums, int k) {
        nth_element(nums.begin(),nums.begin()+k-1,nums.end(),comp);
        return nums(k-1);
    }
};

while doing leetcode question . The only difference between them is i am returning true/false if a==b. This problem doesn’t arise when i use std::sort.