I need to sort a vector of strings, however its not a strait forward sorting. I wrote a custom sort function which works perfectly for smaller vector sizes (<100 element), however I get really weird behavior with larger sizes. Note: the values input are all numbers.
I added some debug printf statements to see what was happening internally which is where I found that empty strings and other strange strings are being passed into the function to be sorted.
My expectation is that only the values in the vector will be passed into the function. I verified that vector is filled with known values.
Sorting function:
bool sortFunc( string a, string b ){
printf( "a: %s,\tb: %s \t", a.c_str(), b.c_str() );
//sorting magic defining 'bool retVal'
printf( "%s goes before %s\n", retVal?a.c_str():b.c_str(), retVal?b.c_str():a.c_str() );
return retVal;
}
Main function:
vector<string> a(n);
for( int i=0; i<n; ++i ){
cin >> a[i];
}
sort(a.begin(),a.end(),sortFunc);
Sample of weird output:
a: 2, b: 10 2 goes before 10
a: 10, b: 10 10 goes before 10
a: , b: 10 goes before 10
a: , b: 10 goes before 10
a: \240E, b: 10 \240E goes before 10
a: , b: 10 goes before 10
a: {@, b: 10 {@ goes before 10
a: , b: 10 goes before 10
a: , b: 10 goes before 10
a: , b: 10 goes before 10
a: \225E, b: 10 10 goes before \225E
a: 2, b: 10 2 goes before 10
a: 10, b: \225E 10 goes before \225E
a: , b: goes before
a: , b: goes before
a: \245E, b: goes before \245E
a: \236E, b: \245E \245E goes before \236E
a: 0G\260\377, b: \236E 0G\260\377 goes before \236E
a: 0G\260\377, b: \245E 0G\260\377 goes before \245E
a: 0G\260\377, b: 0G\260\377 goes before
vector<string> abefore you sort it? Are you sure there are no "weird" values in it.retValis true (it's impossible to tell from the output), you don't have a strict weak ordering and the result is undefined.