如何使用STL算法统计段落中指定位置的单词数?
最佳答案
#include <algorithm>
#include <cctype>
#include <functional>
#include <string>
inline unsigned CountWords( const std::string& s )
{
std::string x = s;
std::replace_if( x.begin(), x.end(), std::ptr_fun <int, int> ( std::isspace ), ' ' );
x.erase( 0, x.find_first_not_of( " " ) );
if (x.empty()) return 0;
return std::count( x.begin(), std::unique( x.begin(), x.end() ), ' ' ) + !std::isspace( *s.regin() );
}
关于algorithm - 使用 STL 的字数统计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9563243/