string容器 Posted on 2020-04-22 | In STL Words count in article: 1.7k | Reading time ≈ 8 string 容器 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259//// Created by Jason on 2020/3/29.///* * string 容器 * string构造函数 string()创建一个空的字符串例如: string str; string( const string&str):/使用一个 string对象初始化另一个 string对象 string( const char*s)://使用字符串s初始化 string( int n, char c)://使用n个字符c初始化 string基本赋值操作 string& operat or=( const char*s)://char*类型字符串賦值给当前的字符串 string& operat or=( onst string &s):/把字符串s赋给当前的字符串 string& operat or=(harc):;/字符赋值给当前的字符串 string& assign( const char*s);/把字符串s赋给当前的字符串 string& assign( const char*s,intn)://把字符串s的前n个字符赋给当前的字符串 string& assign( const string&s);/把字符串s赋给当前字符串 string& assign(int n, char c)://用n个字符c赋给当前字符串 string& assign( const string&s, int st art,intn)://将s从 start开始n个字符赋值给字符串 string 存取字符操作 char*operator[](int n)//通过[]的方式获取字符 char& at (int n)//通过at方法获取字符 3.12.4 string拼接操作 string& operat or+=( const string& str):/重载七操作符 string& operat or+=( const char*str):/重载+操作符 string& operat or+=( canst char c):/重戴+操作符 string& append( const char*s)://把字符串s连接到当前字符串结尾 string& append( const char*s,intn):/把字符串s的前n个字符连接到当前字符串结尾 string& append (const string &3): //B operator=O+ string& append( const string &s, ant pos,intn):/把字符串s中从pos开始的n个字符连接到 当前字符串结尾 string& append(itn, char c)://在当前字符串结尾添加n个字符c3.12.5 string查找和替换 int find( const string& str, ant pos=0) const;/查找str第一次出现位置,从pos开始查找 int find( const char*s, ant pos=0) const;//查找s第一次出现位置,从pos开始查找 int find( const char*s, Int pos,irtn)cnst;∥从pos位置查找s的前n个字符第一次位置 int find( const char c, ant pos=0) const:/查找字符c第一次出现位置4 int rfid(const stringl&str, ant pos=mpos) const:/查找str最后一次位置,从pos开始查找 int find (const char*s, int pos=mpos) const:/查找s最后一次出现位置,从pos开始查找 int rfid(const char*s, lnt pos,intn) const://Npos查找s的前n个字符最后一次位置 int find(const char c, int pos=0) const:/查找字符c最后一次出现位置 replace( ant pos, int n, const string& str)://替换从pos开始n个字符为字符串st string& rep1ace( ant pos,intn, const char*s);//替换从pos开始的n个字符为亨符串s3.12.6 string比较 大写的A比小写的a要小,因为比的是字符串 int compare (const string&s)const//与字符串s比较 int compare(const char*s)const //与字符串s比较3.12.7 string字串 string substr(int pos=0;int n= pos)const//返回由pos 开始的n个字符组成的字符串 string substr(pos)const//返回由pos 开始到字符串结束的字符串3.12.8 string插入和删除操作 string& insert( int pos, const char*s)://插入字符串 string& insert( int pos, const string& str)://插入字符串 string& insert( ant pos,int n, char c):/在指定位置插入n个字符c string& erase( int pos,int n=pos):/刪除从Pos开始的n个字符3.12.9string和c-stye字符串转换 string str="toast";// string转char* const char *cstr= str c_str(); char*s=“ toast";//char*转 string string str(s)补充说明:: transform(a.begin(),a.end(),a.begin(),::toupper)可以将整个字符串中 的英文变成大写! * */#include<bits/stdc++.h>using namespace std;void test01(){ //构造 string s1; string s2(s1);//拷贝构造 string s3("有参构造"); string s4(10,'c');//两个参数的有参 cout<<s3<<endl; cout<<s4<<endl; string s5; s5=s4; s5.assign("abcdefg",3); cout<<s5<<endl; string s6="abcdefg"; string s7; //assign是从0开始计算的 s7.assign(s6,3,3); cout<<s7<<endl;}void test02(){ string s = "hello world";// for(int i = 0;i<s.size();i++)// {// cout<<s[i]<<endl;// cout<<s.at(i)<<endl;// } //at 和中括号的区别:【】访问越界,会直接挂掉,at访问越界,会抛出一个异常 try {// s[100]; s.at(100); } catch (out_of_range&e) { cout<<e.what()<<endl; }}void test03(){ string str1 = " I"; string str2 = " LOVE BEIJING"; str1+=str2; cout<<str1<<endl; string str3 = "天安门"; str1.append(str3); cout<<str1<<endl; string str4 = "abcdefg "; str4.replace(1, 3, "111111");//替换从pos开始n个字符为字符串st cout<<str4<<endl; int pos = str4.find("den"); int pos2 = str4.rfind("de",6);//rfind从右往左找 //查到了,返回第一个数字所在位置 //查不到,返回-1; cout<<"Pos ="<<pos<<endl; cout<<"Pos ="<<pos2<<endl;}void test04(){ string str1 ="abbcde"; string str2="aacdef"; if(str1.compare(str2)==0) { cout<<"str1==str2"<<endl; }else if(str1 .compare(str2)>0) { cout<<"str1>str2"<<endl; }else cout<<"str1<str2"<<endl;}/**********************************************************/void test05(){ string str1 ="abcde"; string subStr = str1.substr(1,3); cout<<subStr<<endl; string email = "2697767160@qq.com"; int pos = email.find("@"); string usrName = email.substr(0,pos); cout<<usrName<<endl;//不需要做+1,-1的操作!}void test06(){ //需求,讲网址中的每个单词都截取到容器当中 string str1 = "www.itcast.com.cn"; vector<string>v; //需要截取 www, itcast , com, cn; int start = 0; while(true) { int pos = str1.find(".",start); if(pos==-1) { //将最后一个单词截取处理 string tmp = str1.substr( start,str1.size()-start); v.push_back(tmp); break; } string tmp = str1.substr( start,pos-start); v.push_back(tmp); start = pos+1; } for(vector<string>::iterator it = v.begin();it!= v.end();it++) { cout<<*it<<endl; }}void test07(){ string str = "hello"; str.insert(1,"111"); cout<<str<<endl; //利用erase 删除掉111 str.erase(1,3); cout<<str<<endl;}void doWork(string s){}void doWork2(const char*s){}void test08(){ //char*->string char*str = "hello"; string s (str); //string -> char* const char*str2 = s.c_str(); doWork(str2);//编译器 将 const char* 隐式转换为string /*doWork2(s);*/ //编译器不会讲string 隐式类型转换为const char*}void test09(){ string s = "abcdefg"; char& a = s[2]; char& b = s[3]; a='1'; b='2'; cout<<s<<endl; cout<<(int*)s.c_str()<<endl; s ="ppppppppppppppppppppppppppppppppppppppppppppppppp";//重新分配内存,之前会失效 a='1'; b='2'; cout<<s<<endl; cout<<(int*)s.c_str()<<endl;}/************************************************************************///把小写字符改成大写字母void test10(){ string str = "abCDEfG"; for(int i = 0;i<str.size();i++) { str[i]= toupper(str[i]); //小写转大写 } cout<<str<<endl; for(int i = 0;i<str.size();i++) { str[i]= tolower(str[i]); //大写转小写 } cout<<str<<endl;}int main(){ test10();// test09();// test08();// test07();// test06();// test05();// test04();// test03();// test01();// test02(); system("pause"); return 0;} -------------本文结束,感谢您的阅读------------- Post author: Jason Post link: https://jasonxqh.github.io/2020/04/22/string%E5%AE%B9%E5%99%A8/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.