小旗的调戏字符串之路就此开始
//总结给自己看的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.youkuaiyun.com/qq_37437983/article/details/80066456
版权声明:本文为博主原创文章,转载请附上博文链接!
insert部分是大佬的五星总结,就直接借鉴来了,详见原文链接,权侵删。
原文:https://blog.youkuaiyun.com/qq_37437983/article/details/80066456
字符串流
需加入头文件
#include <sstream>
stringstream ss;
string s1;
int a;
cin>>s1; //输入字符串,遇行暂停
getline(cin,s1) //读入整行串
ss>>s1;
ss<<a; //用作类型转换