algorithm - 使用 STL 的字数统计

标签 algorithm visual-c++ stl

如何使用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/

相关文章:

c++ - 为什么静态成员函数不能有 cv 限定符?

C++编程帮助!它不会工作?

c++ - 将 emplace 与 std::fill 等算法一起使用

c++ - 如何使用 LuaBind 将 std::map 绑定(bind)到 Lua

c++ - C++ 中只有 2 个不同字符的最长子字符串

algorithm - 所有可能组合的最快解决方案,在 k>2 和 n 大的情况下从 n 个可能的元素中取出 k 个元素

algorithm - 寻找具有特定属性的伪随机数生成算法

c++ - 位反转 : Generating Bit Reversal Lookup Table for N-Bits

c++ - Visual Studio 2012 __cplusplus 和 C++ 11

c - 实现 BFS 返回从顶点 s 到 t 的最小长度路径