Filter out numbers, spaces, and anything else that is not a letter using the correct locale. See this SO thread about handling everything except numbers, like spaces. So use mask and do something similar to what Jerry Coffin offers, but only for letters:
struct alphabet_only: std::ctype<char>
{
alphabet_only(): std::ctype<char>(get_table()) {}
static std::ctype_base::mask const* get_table()
{
static std::vector<std::ctype_base::mask>
rc(std::ctype<char>::table_size,std::ctype_base::space);
std::fill(&rc['A'], &rc['['], std::ctype_base::upper);
std::fill(&rc['a'], &rc['{'], std::ctype_base::lower);
return &rc[0];
}
};
And boom! You are golden.
Or ... you could just do the conversion:
char changeToLetters(const char& input){ return isalpha(input) ? input : ' '; }
vector<char> output;
output.reserve( myVector.size() );
transform( myVector.begin(), myVector.end(), insert_iterator(output), ptr_fun(changeToLetters) );
Which, um, is much easier to disassemble, not as effective as Jerry's idea.
Edit:
Changed "Z" to "[", so the value of "Z" is filled. Similarly from 'z' to '{'.
source
share