请问下面代码为什么无法运行呢?

Andox 2011-04-07 07:14:37

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;

string greet(string form,string lastName,string title,
string::size_type pos,int lenght){
string::iterator beg,end;
beg = form.begin() + form.find("Daily");
end = form.end() - 1;
form.replace(beg,end,lastName);
beg = form.begin() + form.find("Ms");
end = beg + 1;
form.replace(beg,end,title.substr(pos,7));
return form;
}
int main()
{
string generic1("Dear Ms Daisy:");
string generic2("MrsMsMissPeople");
string lastName("AnnaP");
string salute = greet(generic1,lastName,generic2,5,4);
cout << salute << endl;
return 0;
}

以上代码运行后会出现如下提示
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Process returned 3 (0x3) execution time : 0.187 s
Press any key to continue.

还有一个程序运行后也出现相同提示。实在不明白,请各位赐教。
...全文
131 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Andox 2011-04-08
  • 打赏
  • 举报
回复
谢谢大家。看样是我马虎了。
zxs421819166 2011-04-07
  • 打赏
  • 举报
回复
好像那个length没有用到,注释掉就可以运行了,结果是"Dear MissPeos AnnaP:",不知道可是你要得到的结果
机器再学习 2011-04-07
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;

string greet(string form,string lastName,string title,string::size_type pos/*,int lenght*/)
{
string::iterator beg,end;
beg = form.begin() + form.find("Daily");
end = form.end() - 1;
form.replace(beg,end,lastName);
beg = form.begin() + form.find("Ms");
end = beg + 1;
form.replace(beg,end,title.substr(pos,7));
return form;
}
int main()
{
string generic1("Dear Ms Daily:");
string generic2("MrsMsMissPeople");
string lastName("AnnaP");
string salute = greet(generic1,lastName,generic2,5/*,4*/);
cout << salute << endl;
system("pause");
return 0;
}
Freedom 2011-04-07
  • 打赏
  • 举报
回复
你这beg 和end 设置成迭代器 并不是反回的是字符串的起始位置 和结束位置 你何以试试
beg = form.begin() + form.find("Daily");
这个 cout<<beg<<endl;
输入的是
Dear Ms Daisy:
你再好好看看
Aniao 2011-04-07
  • 打赏
  • 举报
回复
beg = form.begin() + form.find("Daily");

这里有问题,最后一个Daily是找不到的,返回的不是你想要的。因为你的字符串是"Dear Ms Daisy:",Daisy,不是Daily

65,210

社区成员

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

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