gpt4 book ai didi

algorithm - 使用 STL 的字数统计

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:03:59 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com