I have this predicate function which compares two strings alphabetically, the strings being compared are human names so are of unequal length, to get round this the shorter string is padded with white space.
Problem:
I've tracked the bug down to the string padding...which appears to randomly break the string iterator:
ls += std::string( maxlen - ls.size(), ' ' ) ; rs += std::string( maxlen - rs.size(), ' ' ) ;
Here is what the two string iterators look like after successful padding, as you can see they both point to their respective string as they should:
& here are the same string iterators further down the list of names being compared, as you can see riter is now pointing to 'ar5' not "Aaron Tasso" which I'm guessing is the cause of the error:
I've tried removing the name "Abraham Possinger" from the input but it throws the same error further down the list on another name.
Input:
Aaron Tasso
Aaron Tier
Abbey Wren
Abbie Rubloff
Abby Tomopoulos
Abdul Veith
Abe Lamkin
Abel Kepley
Abigail Stocker
Abraham Possinger
bool alphanum_string_compare( const std::string& s, const std::string& s2 ) #pragma region CODE { // copy strings: ...for padding to equal length..if required? std::string ls = s ; std::string rs = s2 ; // string iters std::string::const_iterator liter = ls.begin() ; std::string::const_iterator riter = rs.begin() ; // find length of longest string std::string::size_type maxlen = 0 ; maxlen = std::max( ls.size(), rs.size() ) ; // pad shorter of the 2 strings by attempting to pad both ;) // ..only shorter string will be padded!..as the other string == maxlen // ..possibly more efficient than finding and padding ONLY the shorter string ls += std::string( maxlen - ls.size(), ' ' ) ; rs += std::string( maxlen - rs.size(), ' ' ) ; // init alphabet order map static std::map<char, int> m = alphabet() ; //std::map<char, int> m = alphabet(); while( liter != ls.end() && riter != rs.end() ) { if ( m[ *liter ] < m[ *riter ] ) return true ; if ( m[ *liter ] > m[ *riter ] ) return false ; // move to next char ++liter ; ++riter ; } return false ; } #pragma endregion CODE