string函数详解(配案例)

时间:2023-03-09 21:56:58
string函数详解(配案例)

多说无益上码~

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
//1. 字符串的创建
string a(,'a');
cout<<a<<endl<<endl;
//1.以 a 为原字符 4单位大小
string b("bbbbbb");
cout<<b<<endl<<endl;
//2.任意大小的字符串
string c(a,,) ;
cout<<c<<endl<<endl;
//3.把某一字符串的某一部分
//(0位置开始4个长度)给c
system("pause");
system("cls");
//2. 字符串的添加 append();
int num=;
string d(a);//或者 d=d+a;
d.append(b);
cout<<d<<endl<<endl;
//1.在d的末尾添加字符串a
d.append(b,,);
cout<<d<<endl<<endl;
//2.在d的末尾添加字符串
//b(0位置开始,2个长度)的数据
/*貌似还有一种d.appenf(string,int)
结尾添加前int个我编译器有问题?*/
d.append(,'~') ;
cout<<d<<endl<<endl;
//3.添加4个 ~ 字符
//4. 也可以添加 char int 型到句尾(待找...貌似有点问题)
system("pause");
system("cls");
//3. 字符串赋值 assign();
string e,f("");
e.assign(f);
e+=' ';
cout<<e<<endl<<endl;
//感觉就像是append不过是抹除-覆盖
e.assign(f,,);
e+=' ';
cout<<e<<endl<<endl;
//类似
/*貌似还有一种d.assign(string,int)
赋值前int个我编译器有问题?*/
e.assign(,'');
cout<<e<<endl<<endl;
//赋值3个6;
system("pause");
system("cls");
//4. at 只想 位置
char fd;
cout<<e<<endl;
fd=e.at();
cout<<fd<<endl<<endl;
if(a.compare(,,b,,)==)
cout<<a<<endl<<endl;
if(a.compare(b)<)
cout<<b<<endl<<endl;
if(a.compare(,,"aaa",,)==)
cout<<"+++"<<endl<<endl;
return ;
}

先更新几个 占个位置嘛,嘻嘻.