Found in the string in the processing of this string is very useful, just found an article put here. Use string instead of char * array, sort using sort sort algorithm, and use the unique function to go heavy 1, Define string S1 = "Hello"; String s2 = "World"; String s3 = S1 + "," + s2 + "!\n"; 2, append S1 + = ", shanshan\n"; 3, Compare if (S1 = = s2) ..... else if (S1 = = "Hello") ..... 4, string overloaded many operators, including +, + =, <,, =, , [], <<, >>, etc., formal these operators, the string operation is very convenient #include<string> #include<iostream> using namespaceStd intMain () { String Strinfo= "Please input your name:"; cout << Strinfo; Cin >> Strinfo; if(Strinfo = = "Winter" ) cout << "You are winter!"<<endl; Else if(Strinfo! = "Wende" ) cout << "You is not wende!"<<endl; Else if(Strinfo < "Winter") cout << "your name should be ahead of winter"<<endl; Else cout << "your name should is after of winter"<<endl; Strinfo + = ", Welcome to china!"; cout << strinfo<<endl; cout << "Your name is:"<<endl; String strtmp = "How is it ?"+ Strinfo; for(inti = 0; I < strtmp.size (); i + +) cout<<strtmp[i]; return0; } 5. Find function Because lookups are one of the most frequently used features, String provides a very rich look-up function. The list is as follows:
function name |
description |
find |
find |
rfind |
reverse lookup |
find_fi rst_of |
|
find_first_not_of |
find any character that does not contain a substring, return to the first position |
find_last_of |
find any characters that contain substrings, return to the last position |
find_last_not_of |
find any characters that do not contain substrings, return to the last position |
The above functions are overloaded 4 times, the following is the FIND_FIRST_OF function as an example of their arguments, the other functions and their parameters, that is, a total of 24 functions:
Size_type find_first_of (Constbasic_string& s, size_type pos = 0) Size_type find_first_of (Constchart* s, size_type pos, Size_type N) Size_type find_first_of (Constchart* s, size_type pos = 0) Size_type find_first_of (CharT c, size_type pos = 0)
All lookup functions return a size_type type, which is typically the location of the string found and returns String::npos if not found. In fact, String::npos represents-1. Return-1 if not found. Examples are as follows: #include<string> #include<iostream> using namespaceStd intMain () { String Strinfo= "//*---Hello Word!......------"; String Strset= "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"; intFirst = strinfo.find_first_of (Strset); if(first = = String::npos) { cout<< "Not find any characters"<<endl; return-1; } intLast = strinfo.find_last_of (Strset); if(last = = String::npos) { cout<< "Not find any characters"<<endl; return-1; } cout << strinfo.substr (First, Last-first + 1) <<endl;//string.substr is a substring return0; } 6. Insert function, replace function and erase function
The string simply provides the Replace function by location and interval, and cannot replace another string in the specified string with one. Example: #include<string> #include<iostream> using namespaceStd intMain () { String Strinfo= "This is Winter, Winter is a programmer. Do you know Winter?"; cout<< "orign string is: \ n"<<strinfo<<endl; String_replace (Strinfo, "Winter", "Wende"); cout<< "After replace Winter with Wende, the string is: \ n"<<strinfo<<endl; return0; }
String.erase (Pos,srclen);//srclen is the length of the deletion String.Insert (POS,STRDST); POS is positioned, STRDST is the inserted function voidString_replace (String & Strbig,ConstString & Strsrc,ConstString &strdst) { String::size_type pos=0; String::size_type srclen=strsrc.size (); String::size_type dstlen=strdst.size (); while((Pos=strbig.find (STRSRC, pos))! = String::npos) { Strbig.erase (POS, Srclen); Strbig.insert (POS, STRDST); pos + = Dstlen; } } |