私がやろうとしていることの例:
String = "This Is My Sentence"
結果としてこれを取得しようとしています:「TIMS」は、すべての単語の最初の文字のみを取ります。
C++で苦労しています。
Boost Tokenizerを見てください - コードは次のようになります (テストされていません)。
std::string firstLetters(const std::string& str)
{
std::string result="";
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
boost::char_separator<char> sep(" ");
tokenizer tokens(str, sep);
for (tokenizer::iterator tok_iter = tokens.begin();
tok_iter != tokens.end(); ++tok_iter)
{
if (tok_iter->size>0)
{
result+=(*tok_iter)[0];
}
}
return result;
}
または、 Boost String Algorithmsを使用することもできます(これもテストされていません) 。
std::string firstLetters(std::string& str)
{
std::string result="";
std::vector<std::string> splitvec;
boost::split( splitvec, str, boost::is_any_of(" "), boost::token_compress_on );
//C++11: for (std::string &s : splitvec)
BOOST_FOREACH(std::string &s, splitvec)
{
if (s.size()>0)
{
result+=s[0];
}
}
return result;
}
完全を期すためにstrtok関数について言及する必要がありますが、これは C++ よりも C です ;-)
*ジョスト