C++标准模板库STL

STL模板库详细的String及常用函数用法

2020-2-2 寒假期间

string类

1.string类是模板类

typedef basic_string string;

2.string类头文件 #include <cstring>

3.string类对象初始化

string s1("hello");
string s2="hello";
string s3(8,'x');  //8个x

3.string类对象取长度

string s="hello";
int len=s.length();
int len2=s.size();
string支持流读取和getline
string s,s1;
cin>>s;
getline(cin,s1);

4.string类对象赋值与连接 1)“=” 2)assign

string s1="hello",s2,s3,s4;
s2=s1;
s3.assign(s1);
s4.assign(s1,1,3);   //从下标为1的字符开始 复制3个字符赋值s4
对象访问
string s="hello";
for(int i=0;i<s.size();i++)
{	cout<<s[i]<<endl;
cout<<s.at(i)<<endl;    //at会进行范围检查,检查是否越界,s[i]不检查
}
连接
string s1="hello";
string s2="world";
string s=s1+s2;
cout<<s<<endl;
string s3;
s3=s1.append(s2);   //把s2连接在s1后
s3=s1.append(s2,3,5);  //把s2中3到5的字符复制到s1后边  没有足够字符的话到最后一位

5.string类比较

==,>, >= , <,<= , != 直接比较 返回值是bool型
compare 比较 s1.compare(s2),s1大返回1,s1小返回-1 ,相等返回0, s1.compare(1,2,s2,0,3),s1的1-2位与s2的0-3位比较;

6.成员函数

substr 截取子串函数
string s1="hello world";
string s2;
s2=s1.substr(4,5);  //截取s1的4-5位赋值给s2
cout<<s2<<endl;
swap 交换函数
string s1="hello";
string s2="world";
s1.swap(s2);   //交换s1  s2的值
cout<<s1<<s2<<endl;
查找函数find 、rfind、find_first_of 、 find_last_of、
find_first_not _of,find_last_not_of;
string s1="hello";
 cout<<s1.find("l")<<endl; //输出2  s1从前往后找 
 cout<<s1.rfind("l")<<endl;  //输出3 s1从后往前找
 string s="hello worlld";
 cout<<s.find("ll",1)<<endl; //从第1位开始找“ll” 找到返回位置下标
  cout<<s.find("ll",2)<<endl;//从第2位开始找 
 cout<<s.find("ll",3)<<endl;  //从第3位开始找
 /*输出
 2 
 3
 2
 2
 9*/ 
 string s1="hello world";
 cout<<s1.find_first_of("abcdo")<<endl; 
 //从s1中从前往后寻找"abcdo"中的任意字符在s1中第一次出现的位置
 cout<<s1.find_last_of("abcdo")<<endl; 
 cout<<s1.find_first_not_of("abcd")<<endl;
 //从s1中从前往后寻找"abcd"中的任意字符不在s1中第一次出现的位置
 cout<<s1.find_last_not_of("abcd")<<endl;
erase 删除函数
 string s="hello";
 cout<<s.erase(3)<<endl;  //删除下标3及之后的字符
 cout<<s.size()<<endl; 
replace 替换函数
string s1="hello world";
string s2;
 s1.replace(2,3,"Hi"); //s1从2-3的字符替换为Hi
 cout<<s1;
 s2.replace(2,3,"123",1,2); //从s1的2-3个字符替换为“123”的1-2个字符
 /*
 heHi world
 he23 world
 * 
insert 插入函数
 string s1="hello";
 string s3=s1;
 string s2="world";
 s1.insert(2,s2);  //从s1的第2个位置插入s2 
 cout<<s1<<endl;
 cout<<s3.insert(2,s2,2,3)<<endl;
 //从s1的第2个位置插入s2的2位置开始的3个字符
 /*
 heworldllo
 herldllo
 */ 
c_str string类型转换为char *
string s1="hello";
 printf("%s\n",s1.c_str());
 //输出
 hello
copy函数
string s1="hello world";
 int len=s1.length();
 char *p =new char[len+1];
 s1.copy(p,2,0); //从s1 下标位置0开始的长度为2的字符串赋值给p 
 cout<<p<<endl;
 //输出 he 

7.字符串流处理

类似于sscanf 点此转到sscanf
头文件 #include

输入流

string s="hello world 520 13.14 C";
 istringstream ss(s);
 string s1,s2;
 int a;
 double b;
 char c;
 ss>>s1>>s2>>a>>b>>c;
 cout<<s1<<endl;
 cout<<s2<<endl;
 cout<<a<<endl;
 cout<<b<<endl;
 cout<<c<<endl;
 /*输出
 hello
 world
 520
 13.14
 C
 
 */ 
输出流

ostringstream s;
 int a=520;
 s<<"hello "<<a<<" world"<<endl;
 cout<<s.str()<<endl;
 /*
 hello 520 world
 
 */
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值