I am a beginner in C ++, and I am trying to understand the Soundex algorithm that I found somewhere on the Internet. I understand most of this, but it wasn’t explained, it just fit somewhere, so there are a few lines of code that I don’t quite understand.
The Soundex algorithm implemented in the code below is as follows:
http://www.blackwasp.co.uk/soundex.aspx
And here is the code:
#include <algorithm>
#include <functional>
#include <string>
#include <cctype>
using namespace std;
char f_transform( char c )
{
string consonants[ 6 ] = { "BFPV", "CGJKQSXZ", "DT", "L", "MN", "R" };
for (int i = 0; i < 6; i++)
if (consonants[ i ].find( c ) != string::npos)
return (i +1) +'0';
return c;
}
string soundex( const string& s )
{
string result;
if (std::find_if(
s.begin(),
s.end(),
std::not1(std::ptr_fun<int,int>(std::isalpha))
)
!= s.end())
return result;
result.resize( s.length() );
std::transform(
s.begin(),
s.end(),
result.begin(),
std::ptr_fun<int,int>(std::toupper)
);
std::transform(
result.begin() +1,
result.end(),
result.begin() +1,
f_transform
);
result.erase(
std::unique(
result.begin() +1,
result.end()
),
result.end()
);
result.erase(
std::remove_if(
result.begin() +1,
result.end(),
std::not1(std::ptr_fun<int,int>(std::isdigit))
),
result.end()
);
result += "000";
result.resize( 4 );
return result;
}
I get most of this, except for two things: 1. where the "string s check" is done:
if (std::find_if(
s.begin(),
s.end(),
std::not1(std::ptr_fun<int,int>(std::isalpha))
)
!= s.end())
return result;
"ptr_fun". google, . , , "s.begin()" - , , . , isalpha, - . , , , , " " , :).
, , - :
// result.erase( :: ( result.begin() +1, result.end() ), result.end() );
// , result.erase( :: remove_if ( result.begin() +1, result.end(), :: not1 (:: ptr_fun (:: isdigit)) ), result.end() );
, , , .
, "" " , ". "erase" [first, last], , . , , - :
std::unique(result.begin() + 1, result.end() ); ??
""?
:
- "abaace" , "" "abace", ( "abace", "e" ), , . , , . , .