1. Initialization of strings and reading
int main () { string s1= "AAA"; String s2= "BBB"; string s3=s1; Consider the constructor string S4 (' a '); Initialize to "aaaaaaaaaa" cout<<s1<<endl; cout<<s2<<endl; cout<<s3<<endl; cout<<s4<<endl; cout<< "Iterator method" <<endl; for (String::iterator it = S1.begin (); It! = S1.end (); it++) { cout<< *it << "" ; } cout<< "\nat method Take element" <<endl; Use the at FETCH element for (int i=0; i<s1.length (); i++) { cout<<s1.at (i) << " "; Use at to throw an exception } return 0;}
2, get the first address of the string, copy, connect operation
int main () { string s= "ABCD"; printf ("%s\n", S.c_str ()); Get the first address of the string //Copy the contents of S into buf char buf[128]={0}; Initializes a null value of s.copy (buf,3,0); cout<< "BUF:" <<buf<<endl; Connection string s1= "AAA"; String s2= "BBB"; string ss=s1+s2; String s3= "1111"; String s4= "2222"; S3.append (S4); cout<< "SS:" <<ss<<endl; cout<< "S3:" <<s3<<endl; return 0;}
3. String lookup and substitution operations
int main () { ///String lookup and substitution strings s= "Hello, just enjoy the World,hello, just enjoy the World.hello, just enjoy the Wo Rld. "; int Index=s.find ("Hello", 0); Start looking for cout<< "index:" from 0 position <<index<<endl; The number of times to ask Hello to appear int cindex=s.find ("Hello", 0); while (cindex! = string::npos) { cout<< "CIndex:" <<cindex<<endl; S.replace (cindex,5, "HELLO"); The replace operation starts with the CIndex, replacing the 5 characters with "Hello" cindex=s.find ("Hello", cindex+1); } cout<<s<<endl; return 0;}
4. Delete operation of string
int main () { string s= "Hello, world,i hello am Hello walking on Hello the"; String::iterator it = Find (S.begin (), S.end (), ' l '); if (it = S.end ()) { s.erase (it); } The result of cout<< "s deletion is:" <<s<<endl; Specifies the continuous location to remove s.erase (S.begin () +7,s.begin () +15); cout<< "Second Delete:" <<s<<endl; String s2= "Afghi"; S2.insert (1, "BCDE"); cout<< "S2:" <<s2<<endl; return 0;}
5. Application of Transform in string
The std::transform applies to the given operation within the specified range and stores the result in another specified range. To use the Std::transform function you need to include the <algorithm> header file.
int main () { string s1= "aaaabbbbccccdddd"; /* Transform (B1,E1,B2,OP) //Convert the data in an interval [b1,e1] into a second container * Plus: Reason * The problem is the version of Std::tolower inherited from the C standard library was a non-template function, * But there was other versions O F std::tolower that is function templates, * and it's possible for them to being included depending on the standard Library implementation. * You actually want to use the Non-template function, * But there are ambiguity when just tolower is provided as The predicate. * Translation come to say, both C version of the Toupper/tolower function, and STL template function Toupper/tolower, there are conflicts. * /transform (S1.begin (), S1.end (), S1.begin (),:: ToUpper); cout<<s1<<endl; return 0;}
Operation of String in C + +