Defined:
The string class is a template class that is instantiated by the basic_string template in the STL. It is defined as follows:
typedef basic_string<char>string;
constructor function:
The string class has more than one constructor, but does not receive an integer parameter or a constructor for a character parameter
string S1 (); s1= "" String s2 ("Hello"); s2= "Hello" string S3 (4, ' K '); S3= "KKKK" string S4 ("12345", 1,3); s4= "234" i.e. "12345" starting from subscript 1, with a length of 3 substring
String S5 (' K '); Illegal, wrong.
String S6 (123); Illegal, wrong.
Assignment function:
A string object can be assigned with variables of type char*, constants, and variables of type char, constants. For example:
String s1;s1= "Hello"; s1= "Hello" s2= ' k '; S2= "K"
The string class also has a assign member function that can be used to assign a value to a string object. The Assign member function returns a reference to the object itself. For example:
String S1 ("12345"), S2;s3.assign (S1); S3=s1s2.assign (s1,1,2); S2= "23", which is the substring of S1 s2.assign (4, ' K '); S2= "KKKK" S2.assign ("ABCDE", 2,3); S2= "CDE", which is the substring of "ABCDE" (2,3) s2.assign ("12345"). Assign ("678"); Returns a reference to itself, which can continue to be called after the Assign member function s2= "678"
To find the string length:
The length member function returns the lengths of the strings. The size member function can implement the same functionality.
Concatenate strings in a String object:
You can use the "+" and "+ =" operators to concatenate strings on a string object, as well as the string class with the Append member function, or you can add content to the string after it. The Append member function returns a reference to the object itself. For example:
String S1 ("123"), S2 ("abc"); S1.append (S2); s1= "123abc" S1.append (s2,1,2); s1= "123ABCBC" S1.append (3, ' K '); s1= "123ABCBCKKK" S1.append ("ABCDE", 2,3); S1= "123abcbckkkCDE", add "ABCDE" substring (2,3)
Comparison of String objects:
You can compare string objects with the <, <=, = =,,! =, >=, > Operators. The string class also has the Compare member function, which you can use to compare strings. The Compare member function has the following return value: Less than 0 indicates that the current string is small, equals 0 means two strings are equal, and greater than 0 means that the other string is small. For example:
string S1 ("Hello"), S2 ("Hello,world"); int n=s1.compare (S2); N=s1.compare (1,2,s2,0,3) ; Compare the substring (S1) and S2 (0,3) N=s1.compare (0,2,S2) of the string; Compare S1 substrings (0,2) and S2n=s1.compare ("Hello"); N=s1.compare ("Hello"); Compare S1 substrings and "Hello" n=s1.compare ("Hello", "a"); Compare substrings of S1 and "Hello" (Up to)
To find substrings of a string object:
The SUBSTR member function can be used to find a substring (N,M) prototype as follows:
string substr (int n=0,int m=string::npos) const
When called, if the omitted m or m exceeds the length of the string, the substring is the part from the subscript N to the end of the string. For example:
String S1= "This is OK"; string s2=s1.substr (2,4); S2= "is I" s2=s1.substr (2); S2= "is OK"
Swaps the contents of two string objects:
The swap member function can exchange the contents of two Dtring objects. For example:
String S1 ("West"), S2 ("East"); S1.swap (S2); s1= "East" s2= "West"
Find substrings and characters:
The string class has 6 member functions for finding substrings and characters, each of which is named in different forms of find. Their return values are the position of the substring or character in the string object's strings (that is, the value of the subscript string::size_type type). If it is not found, return to String::npos. String::npos is a static constant defined in the string class. String::nops Original no position (no position), is the maximum value of unsigned int uint_max=4294967295, that is, find to the last position also did not match. A value of just 1 under int can also be interpreted as returning a non-existent subscript.
These functions are as follows:
Find: Finds the location of a substring or character in the past.
RFind: Finds the location of a substring or character from behind.
Find_first_of: Find out where to look for characters that appear in another string. For example:
S1.find_first_of ("abc"); Find S1zhong the first time any character in "ABC" appears
Find_last_of: Looks from behind to see where the characters contained in another string appear.
Find_first_not_of: Find out where to look for characters that are not contained in another string.
Find_last_not_of: From back to front, find out where the characters that appear in another string do not contain.
The following is a sample program for the Find member function of the string class:
#include <iostream>
#include <string>
using namespace Std;
int main () {
String S1 ("Source Code");
int n;
if ((N=s1.find (' U '))!=string::npos)//Find where u appears
cout<< "1)" <<n<< "," <<s1.substr (n) <<endl; Output 1) 2,urce Code
if (N=s1.find ("source", 3) ==string::npos)//Find "Source" starting from table 3 below, cannot find
cout<< "2)" << "Not Found" <<endl; Output 2) Not Found
if ((N=s1.find ("co"))!=string::npos)//Find sub-string "Co". Can be found, return the position of "Co"
cout<< "3)" <<n<< "," <<s1.substr (n) <<endl; Output 3) 7,code
if (n=s1.find_first_of ("CEO")!=string::npos)//Find the first occurrence of "C", ' e ' or ' 0 ' position
cout<< "4)" <<n<< "," <<s1.substr (n) <<endl; Output 4) 1,ource Code
if ((n=s1.find_last_of (' e '))!=string::npos)//Find the location of the last ' E '
cout<< "5)" <<n<< "," <<s1.substr (n) <<endl; Output 5) 10,e
if ((N=s1.find_first_not_of ("Eou", 1))!=string::npos)//start with subscript 1 to find the first occurrence of non ' e ', ' o ' or ' u ' character position
cout<< "6)" <<n<< "," <<s1.substr (n) <<endl; Output 6) 3,rec Code
return 0;
}
Replace substring:
The Replace member function can replace substrings in a String object and return a reference to the object itself. For example:
String S1 ("Real Steel"); S1.replace (1,3, "123456", 2,4); Replace the S1 substring (1,3) with the substring of "123456" (2,4) cout<<s1<<endl; Output R3456 Steel string s2 ("Harry Potter"); S2.replace (2,3,5, ' 0 '); Replace the substring with 5 ' 0 ' (2,3) cout<<s2<<endl; Output Ha00000 potterint n=s2.find ("00000"); Find the location of the substring "00000", N=2s2.replace (n,5, "XXX"); Replace the substring (n,5) with "XXX" cout<<s2<<endl; Output Haxxx Potter
To delete a substring:
The Erase member function can delete substrings in a string object, and return a reference to the object itself. For example:
String S1 ("Real Steel"); S1.erase (1,3); Delete the substring (1,3), after which s1= "R Steel" s1.erase (5); Remove the subscript 5 and all characters that follow it. Thereafter s1= "R Ste"
Insert string:
The Insert member function can insert another string in a string object, returning a reference to the object itself. For example:
String S1 ("Limitless"), S2 ("xx"), S1.insert (2, "123"); Insert the string "123" at subscript 2, s1= "Lil123mitless" S1.insert (3,S2); Insert s2,s1= "lil0023mitless" S1.insert (3,5, ' X ') at subscript 2; Insert 5 ' x ' at subscript 3, s1= "Li1xxxxx0023mitless"
To process a string object as a stream:
With Stream objects Istringstream and Ostringstream, you can use a string object as a stream for input and output. These two classes need to contain a header file Sstream. The sample program is as follows:
#include <iostream> #include <sstream> #include <string>using namespace Std;int main () {string src (" Avatar 123 5.2 Titanic K "); Istringstream Istrstream (SRC); Establish the SRC-to-istrstream contact string s1, S2; int n; Double D; char c; Istrstream >> s1 >> n >> d >> S2 >> C; The content of SRC is read as input stream Ostringstream ostrstream; Ostrstream << s1 << endl << S2 << endl << n << endl << d << Endl << C <<endl; cout << ostrstream.str (); return 0;}
The output of this program is:
avatartitanic1235.2k
The 11th line reads from the input stream Istrstream, and the process is read from CIN.
Line 12th, the value of the variable is output to the stream ostrstream. The output does not appear on the screen, but is saved somewhere in the Ostrstream object management. Use the STR member function of the Ostringstream class to extract the contents of the output to the Ostringstream object.
To manipulate a string object with the STL algorithm:
A string object can also be thought of as a sequential container that supports random-access iterators, as well as member functions such as begin and end. Many of the algorithms in the STL also apply to string objects. The following is an example of a program in which the STL algorithm operates a string object:
#include <iostream> #include <algorithm> #include <string>using namespace Std;int main () { string S ("afgcbed"); String::iterator P=find (S.begin (), S.end (), ' C '); if (P!=s.end ()) cout<<p-s.begin () <<endl; Output 3 sort (S.begin (), S.end ()); cout<<s<<endl; Output ABCDEFG next_permutation (S.begin (), S.end ()); cout<<s<<endl; Output ABCDEGF return 0;}
Attention:
1. Unlike character arrays, a string object is fixed in size, meaning that the value of the expression sizeof (string) is fixed, regardless of the length of the string in which it is stored. However, this fixed value is not the same in different compilers. For example, in Dev C + + is 4, in VS2010 is the 32,string object does not directly hold the string, the string will open up memory space elsewhere, the string object value to hold the address of that space, or add some other information.
2. The string object is assigned a value using ordinary characters, and the contents of the ordinary string are copied into the memory space managed by the string object.
3. String objects are compared by dictionary order when comparing size, and are case-sensitive. Because the ASCII code of uppercase letters is less than the ASCII code of lowercase letters (the ASCII code of ' a ' ~ ' Z ' is 0x41~0x5a, the ASCII code of ' a ' ~ ' Z ' is 0x61~0x7a), so ZBC is smaller than ABC.
Reprint Please specify source: http://www.cnblogs.com/goudanli/p/7664213.html
New standard C + + programming
String class------New standard C + + programming