C++ String库及字符串流 函数分类及总结

小旗的调戏字符串之路就此开始
//总结给自己看的blog,可能总结的不太全面,希望不要介意,部分引用了些大佬的总结,权侵删。
字符串

使用字符串需先加入string头文件,并开启名字空间
#inlcude<string>
using namespace std;
  • 删除 erase()
str.erase(a,b)     //删除从a开始删除b个字符
str.erase(a)        //只留前a个字符
  • 排序 sort()
sort(s2.begin(),s2.end())
  • 长度 length()
str.length()           //返回str的长度
  • 是否为空串 empty()
str.empty()      //是返回1,否返回0
  • 取字串 substr()
s1=s2.substr(a,b)    //在s2中,从a开始取b个字符,放到s1中
s1=s2.substr(a)        //在s2中,返回从a到结尾的字串
//不改变原字串
  • 查找 find() ,rfind()
//find函数在找不到指定值得情况下会返回string::npos
//string::npos  表示该类型能达到的最大值,也就是结束值
 s2.find(s1)       //在s2中找s1的值,若匹配返回查找到的第一个位置,否则返回npos
 s2.find(s1,p)     //从p点开始查找匹配的字符,其他与find一致
s2.rfind(s1)		//从s2的尾开始找匹配s1的字串,若找到返回找到的字串在s2的首位置
s2.rfind(s1,p)	//从s2的p位置往前匹配,若匹配,返回找到的字串在s2的首位置
		
such as :string s1="assdfsfd",s2="fd";
		 s1.rfind(s2)=6;
  • 插入 insert
  std::string str="to be question";
  std::string str2="the ";
  std::string str3="or not to be";
  std::string::iterator it;
 
  // used in the same order as described above:
  str.insert(6,str2);                 // to be (the )question
  str.insert(6,str3,3,4);             // to be (not )the question
  str.insert(10,"that is cool",8);    // to be not (that is )the question
  str.insert(10,"to be ");            // to be not (to be )that is the question
  str.insert(15,1,':');               // to be not to be(:) that is the question
  it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question
  str.insert (str.end(),3,'.');       // to be, not to be: that is the question(...)
  str.insert (it+2,str3.begin(),str3.begin()+3); // (or )
--------------------- 
作者:我要出家当道士 
原文:https://blog.csdn.net/qq_37437983/article/details/80066456 
版权声明:本文为博主原创文章,转载请附上博文链接!

insert部分是大佬的五星总结,就直接借鉴来了,详见原文链接,权侵删。
原文:https://blog.csdn.net/qq_37437983/article/details/80066456

字符串流

需加入头文件
#include <sstream>
stringstream ss;
string s1;
int a;
cin>>s1;		//输入字符串,遇行暂停
getline(cin,s1)		//读入整行串
ss>>s1;
ss<<a;		//用作类型转换
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

llllsssq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值