I'm trying to sort an array of strings in C++, but I am getting the following error message:
terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid
The following program causes the previous error. I got the error when v has 17 elements, but everything works fine when v has less elements.
Could someone point me out what is the problem? I'm using gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
bool comp (string s1, string s2) {
if (s1.size() < s2.size())
return false;
else
return true;
}
int main () {
vector<string> v = { "a", "a", "a", "a",
"a", "a", "a", "a",
"a", "a", "a", "a",
"a", "a", "a", "a",
"a" };
sort(v.begin(), v.end(), comp);
return 0;
}