64,654
社区成员
发帖
与我相关
我的任务
分享
int main()
{
vector<string> text;
string text_word;
cout<<"\nInput a text:\n";
while(cin>>text_word)
{
text.push_back(text_word);
}
cin.clear();
cout<<"\n--------So the text you have input is:\n";
displayText(text);
cout<<"\n\n--------After resort, it becomes:\n";
resortText(text);
displayText(text);
cout<<"\n\nNow please insert some new words:\n";
string new_word;
while(cin>>new_word)
{
vector<string>::iterator location=insertLocation(new_word, text);
text.insert(location,1,new_word);
cout<<"\n--------After you have added the new words, the original text has changed into(sorted in the dictionary order):\n";
displayText(text);
}
cin.clear();
}
void resortText(vector<string> &text)
{
vector<string>::iterator beg=text.begin(), end=text.end(),index1=beg,index2=index1;
for(;index1 != end; ++index1)
{
for(index2 = index1+1; index2 != end; ++index2)
{
if(*index1 > *index2)
{
string temp = *index1;
*index1 = *index2;
*index2=temp;
}
}
}
}
vector<string>::iterator insertLocation(string word, vector<string> text)
{
vector<string>::iterator location=text.begin();
while((location != text.end()) && word>*location)
{
++location;
}
return location;
}