C++一个字符串中用find来输出各个单词的问题

2025-12-18 04:21:50
推荐回答(1个)
回答1:

#include
#include
#include
using namespace std;

int main()
{
const string line1 = "We were her pride of 10 she named us:";
const string line2 = "Benjamin, Phoenix, the Prodigal";
const string line3 = "and perspocacious pacific Suzanne";
string sentence = line1 + " " + line2 + " " + line3;
char separator[] = " \t,:\v\r\n\f";
string::size_type pos,count = 0;
string word;
for(;(pos=(sentence.find_first_not_of(separator,count)))!=string::npos;pos++)
{
if(count!=pos&&(count-1)==sentence.find_first_not_of(separator,count-1)) cout< else if(count==pos&&(count-1)!=sentence.find_first_not_of(separator,count-1)) word=sentence.at(pos);
else if(count==pos) word+=sentence.at(count);
count++;
}
}