using namespace std
int main()
{
string s = "ancdr" //为了方便调试观察,改成了r,一样的
//s.assign(s,0,4) //方法1:把字符串s中从0开始的4个字符赋给当前字符串
s.erase(4,1) //方法2:把字符串s中从第4个开始的1个字符删除。
return 0
}
两种方法都可以。
换行符通常是默认的输入结束标志,现在要把它作为字符存入字符串,就得另外约定一个结束符,这里设为'#'。有了这个约定,就有很多办法可以解决题面问题,下面提供一种:
//#include "stdafx.h"//If the vc++6.0, with this line.#include <string>
#include <iostream>
using namespace std
int main(void){
string s
char x
while((x=cin.get())!='#')//输入#结束,其他字符继续
s+=x
cout << s << endl
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)