大家给看看

lrd408 2008-11-15 05:44:26
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
string line1="We were her prede of 10 she named us: ";
string line2="Benjamin, Phoenix, the Prodegal";
string line3="and perspicacious pacific Suzanne";
string sentence=line1+' '+line2+' '+line3;
string separators=" \t:,\v\r\n\f";
string word;
string::size_type starpos=0,endpos=0,maxlen,minlen,count=0,wordlen;
vector<string> maxvec,minvec;
while((starpos=sentence.find_first_not_of(separators,endpos))!=string::npos)
{
++count;
endpos=sentence.find_first_of(separators,starpos);
if(endpos==string::npos)
wordlen=sentence.size()-starpos;
else wordlen=endpos-starpos;

word.assign(sentence.begin()+starpos,sentence.end()+starpos+wordlen);
//starpos=sentence.find_first_not_of(separators,endpos);
if(count==1)
{maxlen=wordlen;
minlen=wordlen;
maxvec.push_back(word);
minvec.push_back(word);
}
else {
if(wordlen>maxlen)
{
maxvec.clear();
maxlen=wordlen;
maxvec.push_back(word);
}
else if(wordlen==maxlen)
maxvec.push_back(word);
if(wordlen<minlen)
{minvec.clear();
minlen=wordlen;
minvec.push_back(word);
}
else
minvec.push_back(word);
}
}

cout<<"word amount: "<<count<<endl;
vector<string>::iterator iter;

cout<<"longest word(s):"<<endl;
iter=maxvec.begin();
while(iter!=maxvec.end())
cout<<*iter++<<endl;

cout<<"shotest word(s):"<<endl;
iter=minvec.begin();
while(iter!=minvec.end())
cout<<*iter++<<endl;
return 0;
}

得出的结果:不是想要的 不知道为什么
题目:已知有如下 string 对象:

string line1 = "We were her pride of 10 she named us:";
string line2 = "Benjamin, Phoenix, the Prodigal"
string line3 = "and perspicacious pacific Suzanne";

string sentence = line1 + ' ' + line2 + ' ' + line3;



counts the number of words in sentence and identifies the largest and smallest words. If several words have the largest or smallest length, report all of them.

编写程序计算 sentence 中有多少个单词,并指出其中最长和最短的单词。如果有多个最长或最短的单词,则将它们全部输出。
...全文
129 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangkunhenry 2008-11-15
  • 打赏
  • 举报
回复
这个!!!

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
string line1="We were her prede of 10 she named us:";
string line2="Benjamin,Phoenix,the Prodegal";
string line3="and perspicacious pacific Suzanne";
string sentence=line1+' '+line2+' '+line3;
string separators=" \t:,\v\r\n\f";
string word;
string::size_type starpos=0,endpos=0,maxlen,minlen,count=0,wordlen;
vector<string> maxvec,minvec;
while((starpos=sentence.find_first_not_of(separators,endpos))!=string::npos)
{
++count;
endpos=sentence.find_first_of(separators,starpos);
if(endpos==string::npos)
wordlen=sentence.size()-starpos;
else
wordlen=endpos-starpos;
word.assign(sentence.begin()+starpos,sentence.begin()+starpos+wordlen);//Here----sentence.begin()
//starpos=sentence.find_first_not_of(separators,endpos);
if(count==1)
{
maxlen=wordlen;
minlen=wordlen;
maxvec.push_back(word);
minvec.push_back(word);
}
else
{
if(wordlen>maxlen)
{
maxvec.clear();
maxlen=wordlen;
maxvec.push_back(word);
}
else if(wordlen==maxlen)
maxvec.push_back(word);
if(wordlen<minlen)
{
minvec.clear();
minlen=wordlen;
minvec.push_back(word);
}
else if(wordlen==minlen)//--------Here
minvec.push_back(word);
}
}

cout<<"word amount: "<<count<<endl;
vector<string>::iterator iter;

cout<<"longest word(s):"<<endl;
iter=maxvec.begin();
while(iter!=maxvec.end())
cout<<*iter++<<endl;

cout<<"shotest word(s):"<<endl;
iter=minvec.begin();
while(iter!=minvec.end())
cout<<*iter++<<endl;
return 0;
}
yangkunhenry 2008-11-15
  • 打赏
  • 举报
回复

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
string line1="We were her prede of 10 she named us:";
string line2="Benjamin,Phoenix,the Prodegal";
string line3="and perspicacious pacific Suzanne";
string sentence=line1+' '+line2+' '+line3;
string separators=" \t:,\v\r\n\f";
string word;
string::size_type starpos=0,endpos=0,maxlen,minlen,count=0,wordlen;
vector<string> maxvec,minvec;
while((starpos=sentence.find_first_not_of(separators,endpos))!=string::npos)
{
++count;
endpos=sentence.find_first_of(separators,starpos);
if(endpos==string::npos)
wordlen=sentence.size()-starpos;
else
wordlen=endpos-starpos;
word.assign(sentence.begin()+starpos,sentence.begin()+starpos+wordlen);//Here----sentence.begin()
//starpos=sentence.find_first_not_of(separators,endpos);
if(count==1)
{
maxlen=wordlen;
minlen=wordlen;
maxvec.push_back(word);
minvec.push_back(word);
}
else
{
if(wordlen>maxlen)
{
maxvec.clear();
maxlen=wordlen;
maxvec.push_back(word);
cout<<word<<endl;
system("pause");
}
else if(wordlen==maxlen)
maxvec.push_back(word);
if(wordlen<minlen)
{
minvec.clear();
minlen=wordlen;
minvec.push_back(word);
}
else if(wordlen==minlen)//Here
minvec.push_back(word);
}
}

cout<<"word amount: "<<count<<endl;
system("pause");
vector<string>::iterator iter;

cout<<"longest word(s):"<<endl;
iter=maxvec.begin();
while(iter!=maxvec.end())
cout<<*iter++<<endl;
system("pause");

cout<<"shotest word(s):"<<endl;
iter=minvec.begin();
while(iter!=minvec.end())
cout<<*iter++<<endl;
return 0;
}
太乙 2008-11-15
  • 打赏
  • 举报
回复
汗~~~~~~不用这么复杂吧??

内容概要:本文针对考虑算力负荷时空迁移特性的多微电网与共享储能系统的协同优化调度问题展开研究,并提供了完整的Matlab代码实现。研究构建了融合算力负荷动态迁移特征的数学优化模型,通过引入共享储能机制,实现多微电网间的能量互济与资源协同,有效提升系统对分布式能源波动性和负荷不确定性的适应能力。文中采用先进的优化算法求解该调度模型,重点解决了算力任务在时空维度上的灵活调配与电力供需平衡之间的耦合关系,旨在提高综合能源系统的运行经济性、可靠性与灵活性。所提出的方法为未来能源互联网背景下电-算协同管理提供了理论支持与技术路径。; 适合人群:具备电力系统分析、优化理论基础及Matlab编程能力的科研人员、高校研究生,尤其适用于从事微电网运行、共享储能配置、综合能源系统优化以及电-算融合等领域研究的专业技术人员。; 使用场景及目标:①开展多微电网与共享储能系统的协同调度策略设计与仿真验证;②支撑高比例可再生能源接入下的新型电力系统优化运行研究;③为考虑算力迁移的数据中心与电网协同调度提供建模与算法参考;④作为高水平学术论文撰写或学位课题研究的技术支撑与代码复现平台。; 阅读建议:建议读者结合Matlab代码与相关学术文献深入研读,重点关注目标函数构建、约束条件设定及求解器调用逻辑,可在现有模型基础上拓展多时间尺度优化、不确定性建模(如鲁棒优化、随机规划)或加入实际工程约束进行二次开发与深化研究。

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧