I have an array of strings
string relationarray[10000][2];
taken from #include<string>. Every string at relationarray[index][0] of the array has a 4 digit number at its beginning that I'm trying to use to sort the array. The array is usually not full.
from #include<algorithm>I'm trying to use
std::sort(relationarray,relationarray + (sizeof(relationarray)/sizeof(relationarray[0])) to get eveyrthing in order.
but sort puts the strings at the end of the array in favor of null positions. What am I doing wrong? I tried creating comparison function for the third argument of sort but it doesn't compile.
bool waytosort(string one, string two){
if (two.empty()){
return false;
}
int first =stoi(one.substr(0,3));
int second=stoi(two.substr(0,3));
return first<second;
}
std::sort? That is an STL algorithm function.std::map<string, string>orstd::map<int, string>and none of this code would need to be written.I've heard that qsort isn't part of the STL but I don't know how to use it.And you shouldn't use it. Theqsortknows nothing about C++ classes or non-POD types. Therefore there is no guarantee that whenqsortstarts swapping your data, it isn't mangling it beyond repair. Read the Notes section here: en.cppreference.com/w/cpp/algorithm/qsort