一个合并string串的代码,求大侠解答
//将写入的string对象连接起来汇总成一个总的string变量。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s_result,s;
cout<<"please enter string "<<endl;
while (cin>>s)
s_result=s_result+s;
cout<<"s_result is "<<s_result<<endl;
return 0;
}
//修改为string中间加一个空格
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s_result,s;
cout<<"please enter string "<<endl;
cin>>s_result; /*为什么这里多了一个队s_result赋值,而上例却没有呢*/while (cin>>s)
s_result=s_result+' '+s;
cout<<"s_result is "<<s_result<<endl;
return 0;
}
[解决办法]
第一个程序s_result初始为空, 然后每输入一个s 就把s_result 与 s合并。一开始只需要一个字符串;
第二个程序是在两个字符串中间加空格,一开始需要两个字符串,所以两个都输入初始字符串。
[解决办法]
会有结果输出么?感觉while语句都没跳出的明显条件。