跟着网课自学蓝桥杯,复习一下C++的函数知识,代码如下:
int main(){
string str1="abcd";
//输出字符串
for(int i=0;i<str1.size();i++){
cout<<str1[i];
}
//字符串复制
string str2 =str1;
cout<<str2<<endl;
//字符串拼接1
string str3=str1+str2;
cout<<str3<<endl;
//字符串拼接2
string s1="hello";
string s2="world";
string s3=s1.append("abcd",2);//指定拼接两个字符
cout<<s3<<endl;
//查找(找到返回位置,从0开始,找不到返回-1)
string st1="abcdef";
string st2="ab";
int ans=st1.find(st2);
cout<<ans<<endl;
//子串
st2=st1.substr(1,4); //截取从1位置开始的4位字串
cout<<st2<<endl;
st2=st1.substr(3); //截取从3位置开始的字串
cout<<st2<<endl;
//比较
int a = s1.compare(s2);//不同返回-1,相同为0
cout<<a;
//数字字符相加
string a,b;
int carry;
while(a.size()<b.size()) a='0'+a;
while(a.size()>b.size()) b='0'+b;
return 0;
}