string类 添加
作者:
东.
,
2022-11-30 16:46:43
,
所有人可见
,
阅读 194
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string a,b;
cin >> a >> b;
//拼接
a += b;
cout << a << endl;
a.append(b);
cout << a << endl;
a.append("hi",1);
a.append(b,0,1);
cout << a << endl;
//查找
int pos1 = a.find('l');
cout << pos1 << endl;
int pos2 = a.rfind('l');
cout << pos2 << endl;
//比较
if(a.compare(b) == 0) cout << "=" << endl;
if(a.compare(b) > 0) cout << ">" << endl;
if(a.compare(b) < 0) cout << "<" << endl;
//存取
for(int i = 0; i < a.size(); i ++)
cout << a[i] << " " ;
cout <<endl;
for(int i = 0; i < a.size(); i ++)
cout << a.at(i) << " " ;
cout << endl;
//修改单个字符
a[1] = 'o';
//插入
a.insert(1,b);
cout << a << endl;
//删除
a.erase(1,2);
cout << a << endl;
//截取子串
string c = a.substr(2,4);
cout << c << endl;
return 0;
}