比较缩写
string引用作为形参

#include
#include

using namespace std;

int source(string &input)
{
string tmp=input;
input[0]=tmp[0];
int j=1;
for(int i=1;i<tmp.size();i++)
{
if(tmp[i]!=tmp[i-1])
{
input[j++]=tmp[i];
}
}
return j;
}

int main()
{
string str;
while(cin>>str)
{
string first=str;
//str.clear();
cout<<“strsize =”<<str.size()<<endl;
cout<<"&str ="<<&str<<endl;
cout<<"&first ="<<&first<<endl;
int len = source(first);
string a = first.substr(0,len);
cout<<“first =”<<first<<endl;
cout<<“a =”<<a<<endl;
cout<<“str =”<<str<<endl;
int num = 0;
string input;
for(int i=0;i<4;i++)
{
cin>>input;
cout<<"&input ="<<&input<<endl;
string second=input;
cout<<"&second ="<<&second<<endl;
int len = source(second);
cout<<“second =”<<second<<endl;
cout<<"&input ="<<&input<<endl;
cout<<"&second ="<<&second<<endl;
cout<<“input =”<<input<<endl;
string b = second.substr(0,len);
cout<<“b =”<<b<<endl;
if(a==b)
{
num++;
}
}
cout<<“num =”<<num<<endl;
cout<<"str "<<str<<endl;
}
return 0;
}

!](https://img-blog.csdnimg.cn/20200728181952539.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MDQ4NjA0Mg==,size_16,color_FFFFFF,t_70)
C++ string作为形参的使用
当执行string first=str;之后,first的地址和str不一样了,对first进行操作也不会影响str的值。
利用string的substr(start,end)来取出想要的子串。

相关文章:

  • 2021-06-17
  • 2022-12-23
  • 2022-12-23
  • 2021-06-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-24
  • 2022-12-23
  • 2021-05-15
  • 2021-12-10
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案