求一段代码的实现

rome55 2010-03-13 06:17:41
定义string 然后让用户输入,然后已字符递减的形式输出(如果字符长度相同的话,比较ASCII码输出)
最后在输出所有连一起的句子
『不是作业,我就看到C++书了,做完习题了,然后我想自己扩展一下,就碰到问题了啊,以前是用int 定义,递减的时候输入--就行,但是string 我用-- 就报错了,用while接受>> 希望大家帮忙一下啊 感激不尽 感激不尽啊』

我是这样写的
int main()
{
cout<<"Enter strings\n";
string st;
while(cin>>st)
{
cout<<st<<endl;
//st--;
}
return 0;
}


报错,谁能写一个完整的啊 感激不尽 啊
...全文
135 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiedi1209 2010-03-14
  • 打赏
  • 举报
回复
string没有--这个操作符
rome55 2010-03-14
  • 打赏
  • 举报
回复
你写那玩意,那么简单 我用你写啊?[Quote=引用 11 楼 kao759620299yan 的回复:]
#include <iostream>

using namespace std;

int main()
{
cout<<"Enter strings\n";
string st;
while(cin>>st)
{
cout<<st<<endl;
}
return 0;
}
C++不支持字符串的操作,你可以重载它
[/Quote]
未知年代 2010-03-13
  • 打赏
  • 举报
回复
#include <iostream>

using namespace std;

int main()
{
cout<<"Enter strings\n";
string st;
while(cin>>st)
{
cout<<st<<endl;
}
return 0;
}
C++不支持字符串的操作,你可以重载它
lovesi3344 2010-03-13
  • 打赏
  • 举报
回复
析构函数不是virtual,会造成局部析构
  • 打赏
  • 举报
回复
string没有--这个操作符。你可以写个my_string,并重载--符号。
yyg990441 2010-03-13
  • 打赏
  • 举报
回复
楼上的说得对!这样设计MyStr确实不好,但是在这个例子中这样用不会造成局部析构.
只有如下使用才会:
string *p = New MyStr;
...
delete p;//这里局部析构
BT六眼飞鱼 2010-03-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yyg990441 的回复:]

引用 3 楼 rome55 的回复:
能不能写一个完整的例子啊

引用 2 楼 whut_lcy 的回复:
string不支持操作符。你可以自己重载它

我替他写个
C/C++ code

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

class MyString:……
[/Quote]
晕,string的析构函数不是virtual的。。。这么做会造成局部析构的
rome55 2010-03-13
  • 打赏
  • 举报
回复
可以写一个例子吗 谢谢了[Quote=引用 7 楼 hairetz 的回复:]
string没有--这个操作符。你可以写个my_string,并重载--符号。
[/Quote]
yyg990441 2010-03-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 rome55 的回复:]
能不能写一个完整的例子啊

引用 2 楼 whut_lcy 的回复:
string不支持操作符。你可以自己重载它
[/Quote]
我替他写个

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

class MyString:public string{
public:
MyString operator--(int){
sort(this->begin(),this->end());
return *this;
}
};

int main(){
MyString Mstr;
cin >> Mstr;
Mstr--;
cout << Mstr << endl;
}
rome55 2010-03-13
  • 打赏
  • 举报
回复
能不能写一个完整的例子啊[Quote=引用 2 楼 whut_lcy 的回复:]
string不支持操作符。你可以自己重载它
[/Quote]
whut_lcy 2010-03-13
  • 打赏
  • 举报
回复
string不支持操作符。你可以自己重载它
one2zero 2010-03-13
  • 打赏
  • 举报
回复
字符串 -- 想做什么...
先放进数组

或者用指针
sonnno 2010-03-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 yyg990441 的回复:]

楼上的说得对!这样设计MyStr确实不好,但是在这个例子中这样用不会造成局部析构.
只有如下使用才会:
string *p = New MyStr;
...
delete p;//这里局部析构
[/Quote]

能详细点告诉我为什么这里不会造成局部析构吗 谢谢了

64,646

社区成员

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

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