/*============================================string is a string type of STL that is commonly used to represent strings ==================================== =========*/#include<iostream>using namespacestd; #include"string"//header file for string#include"algorithm"/*initialization of the ============================================string object =================================== ==========*/voidmain21 () {stringS1 ="AAA";//The first method of initialization stringS2 ("BBB");//the second method of initialization strings3 = S2;//initialize an object by copying the constructor S3 stringS4 (Ten,'a');//initialize with n characters of Ccout<<"S1:"<<s1<<Endl; cout<<"S2:"<<s2<<Endl; cout<<"S3:"<<s3<<Endl; cout<<"S4:"<<s4<<Endl;}/*Traversal of ============================================string ================================ =============*/voidMain22 () {stringS1 ="ABCDEFG"; //1. Array mode for(inti =0; I<s1.length (); i++) {cout<<s1[i]<<" ";//An error does not throw an exception, causing the program to break } //2. By way of Iterators for(string:: Iterator it = S1.begin (); It! = S1.end (); it++) {cout<<*it<<" "; } cout<<Endl; //3.string at Mode for(inti =0; I<s1.length (); i++) {cout<<s1.at (i) <<" ";//can throw an exception } Try { for(inti =0; I<s1.length () +3; i++) {cout<<s1.at (i) <<" "<<endl;//can throw an exception } } Catch (...) {cout<<"An exception occurred"<<Endl; }}//conversion of character pointers and stringsvoidmain23 () {stringS1 ="AAA BBB";//char*====>string//S1====>char *printf"s1:%s\n", S1.c_str ()); //char*====>string//S1 copy of content to BUF Charbuf1[ -] = {0}; S1.copy (BUF1,3,0);//Note that only copy3 characters will not change to a C-style stringcout <<"BUF1:"<<buf1<<Endl;}//Link StringvoidMain24 () {stringS1 ="AAA"; stringS2 ="BBB"; S1= S1 +S2; cout<<"S1:"<<s1<<Endl; stringS3 ="333"; stringS4 ="444"; S3.append (S4); cout<<"S3:"<<s3<<Endl;}//Search and replace of stringsvoidMain25 () {stringS1 ="WBM Hello WBM 111 wbm 222 WBM 333"; //first occurrence of WBM index intindex = S1.find ("WBM",0);//The position subscript is starting from 0.cout<<"Index:"<<index<<Endl; //WBM Each occurrence of an array subscript intOffindex = S1.find ("WBM",0); while(Offindex! =string:: NPOs) {cout<<"Offindex:"<<offindex<<Endl; Offindex= Offindex +1; Offindex= S1.find ("WBM", Offindex); } //Case 2 stringS3 ="AAA BBB CCC"; S3.replace (0,3,"AAA"); cout<<"S3:"<<s3<<Endl; Offindex= S1.find ("WBM",0); while(Offindex! =string:: NPOs) {cout<<"Offindex:"<<offindex<<Endl; S1.replace (Offindex,3,"WBM"); Offindex= Offindex +1; Offindex= S1.find ("WBM", Offindex); } cout<<"S1:"<<s1<<Endl;}//truncation (interval deletion) and insertionvoidmain26 () {stringS1 ="Hello Hello2 hello1"; //string &eras (int pos = 0,int n = pos); Deletes the n characters starting at POS and returns the modified string string:: Iterator it = Find (S1.begin (), S1.end (),'L'); if(It! =S1.end ()) {s1.erase (IT); } cout<<"S1 after deleting the result:"<<s1<<Endl; S1.erase (S1.begin (), S1.end ()); cout<<"S1 after deleting the result:"<<s1<<Endl; cout<<"S1 Length:"<<s1.length () <<Endl; stringS2 ="BBB"; S2.insert (0,"AAA");//Head Insertion Methodcout<<"S2 Results:"<<s2<<Endl; S2.insert (S2.length (),"CCC");//tail interpolation methodcout<<"S2 Results:"<<s2<<Endl;}voidmain27 () {stringS1 ="aaabbb"; //1. Entry Address of Function 2. function Object 3. Predefined functionsTransform (S1.begin (), S1.end (), S1.begin (), toupper);//Turn all uppercasecout <<"S1:"<<S1 <<Endl; stringS2 ="aaabbb"; Transform (S1.begin (), S1.end (), S2.begin (), tolower); //change all to lowercasecout <<"S2:"<< S2 <<Endl;}intMain () {//main21 (); //Main22 (); //main23 (); //Main24 (); //Main25 (); //main26 ();main27 (); return 0;}
C++stl Learning Notes _ (1) string knowledge