c++ string类型和int类型的相互转换
一、使用atoi()函数
string str="123"; int num = atoi(str); double num = atof(str); long num = atol(str);
二、使用to_string()函数
int num=123; string str=to_string(num); cout<<"num="<<num<<" num的类型为"<<typeid(num).name()<<endl;//输出i表示int类型 cout<<"str="<<str<<" str的类型为"<<typeid(str).name()<<endl;//输出Ss表示string;类型
三、使用itoa()函数
/* 第一个参数是要转换的数字 第二个参数是要写入转换结果的目标字符串(字符型数组) 第三个参数是转移数字时所用的基数(进制)⭐(可以用来做进制转换) */ int num=123; char strc[100]; string str=itoa(num,strc,10);//返回的是指向strc的指针,直接存进string类型即可 cout<<"num="<<num<<" num的类型为"<<typeid(num).name()<<endl; cout<<"str="<<str<<" str的类型为"<<typeid(str).name()<<endl;
c++ string的用法
size(),length(),capacity(),empty(),clear(),reserve(),resize();
string插入和拼接
-
string插入
1:在字符串末尾插入 void push_back(char c);//向字符串末尾追加一个字符 2:在指定位置插入 string& insert(size_t pos,const string& str);//插入字符串拷贝 string& insert(size_t pos,const char* s);//插入c形式的字符串 string& insert(size_t pos,const char* s, size_t n);//将字符串的前n个字符插入到pos位置 string s0(""); s0.push_back('h');//s0尾插h cout<< s0<<endl; string s1("ell"); s0.insert(1,s1);//在下标为1的位置插入s1的拷贝 cout<< s0 <<endl; s0.insert(4,"o");//在下标为4的位置插入字符串o; cout<< s0 <<endl; s0.insert(0,"cplusplus",2);//在下标为0的位置插入“cplusplus"的前2个字符 cout<< s0 <<endl; h hell hello cphello
-
string 拼接
string s0(""); s0.append("hello"); //operator+: string s1="hello"; string s2="abc"; string s3=s1+s2; //helloabc; s1+='!'; //hello!;
-
stirng删除
string& erase(size_t pos=0,size_t len = npos);//从第pos个位置开始删除len个字符,pos省略代表0
-
string容量
-
size() length();
-
判断是否为空: s1.empty();
-
容量:s1.capacity()>s1.size() capacity包括'\0'
-
调整字符串大小
-
void resize(size_t n);//将字符串调整为n个字符的长度用'\0'填充
-
void resize(size_t n,char c);//将字符串大小调整为n个字符的长度,初始化新增空间的内容为c
-
-
调整字符串容量:void reserve(size_t n=0);//调整容量,缺省容量为0
-
-
string字符串操作
-
获得c形式字符串
const char* c_str() const;//将const string* 类型 转化为 const char* 类型 string s1("cplusplus"); /*s1 resize 之后,在结尾插入“!!!”,需要重开空间*/ s1.resize(30); s1+="!!!"; string s2 = s1.c_str(); cout<<s1.size()<<endl;//33 cout<<s2.size()<<endl;//9 s1的c形式字符串,遇到第一个'\0'就截止了
-
查找
size_t find(const string &str,size_t pos = 0) const;//返回在给定字符串中查找字符串str第一次出现的位置 size_t find(const char* s,size_t pos = 0) const;//返回在给定字符串中从下标为pos的位置开始查找字符串s第1次出现的位置 size_t find(const char* s,size_t pos,size_t n) const;//返回在给定字符串中从下标为pos的位置开始查找字符串s的前n个字符第一次出现的位置 size_t find(const c,size_t pos = 0) const;//返回在给定字符串中从下标为pos的位置开始查找字符c第一次出现的位置 string s1("cplusplus.co"); cout<<s1.find("lu");//2 cout<<s1.find("lu",3);//6 cout<<s1.find('.',3);//9 cout<<s1.find("com",3,2);//10
-
查找子串
string substr(size_t pos=0,size_t len = npos) const;//截取给定字符串中第pos个位置开始的len个长度的字符
-
反向查找
size_t rfind(const string & str, size_t pos = npos) const;//从右向左在给定字符串中查找字符串str第一次出现的位置 size_t rfind(const char* s, size_t pos = npos) const;//从右向左在给定字符串中下标为pos的位置开始查找字符串s第一次出现的位置 size_t rfind(const char* s, size_t pos, size_t n) const;//从右向左在给定字符串中从下标为pos的位置开始查找字符串s的前n个字符第一次出现的位置 size_t rfind(char c, size_t pos = npos) const;//从右向左在给定字符串中从下标为pos的位置开始查找字符c第一次出现的位置 string url1 = "https://cplusplus.com/reference/string/string/rfind:/"; cout << url1.rfind(':') << endl;//51
-
比较
-