第一章查找单词例题-算法设计与分析报告C/C++版

设计如图

PS:上面的设计报告pdf文档已上传,需要可以下载 

完整代码如下 

//author:rgh
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void solve(string str,vector<string>&words)//产生所有单词
{
string w;
int i=0;
int j=str.find(" ");// 查找第一个空格
while(j!=-1)//找到单词后循环
{
w=str.substr(i,j-i);//提取一个单词
words.push_back(w);//将单词添加到words中
i=j+1;
j=str.find(" ",i);//查找下一个空格 
}

if(i<str.length()-1)//处理最后一个单词
{
w=str.substr(i);//提取最后一个单词
words.push_back(w);//最后单词添加到words中
}
}
int main()
{
	string str="The following code computes the intersection of two arrays";
	vector<string>words;
	solve(str,words);
	cout<<"所有的单词:"<<endl;//输出结果
	vector<string>::iterator it;
	for(it=words.begin();it!=words.end();++it)
	cout<<" "<<*it<<endl;
	return 0; 
	}

喜欢的话关注一下吧,打赏也行

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章