String
A string class is defined in the C + + standard library to handle variable long character sequences. Use the string class to include the header file, which is defined in the namespace Std;
#include <string> using namespace std;
1. Definition and initialization of string
string str1; Define
string str2 = str1; Copy Assignment
string str3 = "ABCDE"; Direct Assignment
string STR4 (char* str); String pointer initial string
STR5 (5, ' C '); The content of STR5 is CCCCC
2. String Object Basic operation
CIN >> str; Standard input
cout << str; Standard output
str.empty (); Whether the string is an empty
str.size (); String size
str.length (); String length as str[n as size
]; A string of nth characters, similar to an array
str.c_str (); String Type char*
3, string operator
STR1+STR2 //string concatenation, string can also be directly plus a character
str1 = = str2 //String equality
str1!= str2 //String inequality
< > <= >= //String size comparison, case-sensitive
4. Precautions and common functions
The traversal and array of string is similar to Str[i], where a char-type character is obtained, and when acquired with the address &str[i], the character and subsequent characters are acquired, not one character.
Connection functions (if the char type is generally a pointer, the string type is generally a reference, and the reference is returned):
String &operator+= (const string &s);//Connect the string s to the end of the current string
String &append (const string &s); With operator+= ()
String &append (int n,char c); Add n characters at the end of the current string C
String &append (const string &s,int pos,int N) fonts to the end of the current string with n words starting from POS in string s
String &append (const char *s); Connect the C type string s to the end of the current string
String &append (const char *s,int n);//The first n words of the C type string s are fonts to the end of the current string
Sub string:
String substr (int pos = 0,int n = NPOs) const;//returns the number of n characters at the beginning of the POS, with the default to the end of the string without indicating the second argument
Lookup (successfully returned location, no return string::npos found):
int find (char c, int pos = 0) const;//Look for the character C at the position of the current string from the POS
int find (const string &s, int pos = 0) const;//The position of the string s in the current string starting from Pos
int find (const char *s, int pos = 0) const;//The position of the string s in the current string starting from Pos
int find (const char *s, int pos, int n) const;//The position of the first n characters in the string s in the current string starting from the POS
Reverse Lookup:
int RFind (char c, int pos = NPOs) const;//Find the position of the character C in the current string from the beginning of the POS
int RFind (const string &s,int pos = NPOs) const;
Returns the location of the String::npos when the lookup succeeds, and the failure returns the value of the
Exchange:
void Swap (string &s2); Swap the value of the current string and S2
Replace:
String &replace (int p0, int n0,const string &s)//delete p0 characters starting from N0, then insert string s at P0
String &replace (int p0, int n0,const string &s, int pos, int n);//delete p0 start n0 characters, then insert n characters from Pos in string s at P0
String &replace (int p0, int n0,int n, char c);//delete n0 characters starting p0, then insert n characters at P0
Insert:
String &insert (int p0,const string &s);
String &insert (int p0,const string &s, int pos, int n);//at p0 position Insert the first n characters of the Pos beginning in the string s
String &insert (int p0, int n, char c);//This function inserts n characters c at p0
Delete:
String &erase (int pos = 0, int n = npos);//delete the N-character at the beginning of the POS, return the modified string
Many functions are also defined in the string class for different types of processing and applications of string.
Detailed reference: http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html
5, the relationship between string,ctring,string.h
To understand the relationship between these headers, you first need to understand C + + and C header file Relationship: C + + standard library In addition to the definition of C + + language-specific features, but also compatible with the C language standard library, C language header files such as name.h,c++ will define these files as CNAME. is to remove the. h, add c before the filename.
So the contents of Cctype and ctype.h headers are the same, except that they are more in line with the C + + specification from the naming conventions, and, in particular, the names defined in CNAME header files are subordinate to the namespace STD, and. h is not. In general, C + + is best to start with a file rather than. h, which is more in line with the specification, and the. h doesn't matter, the content is the same.
From the above, CString and string.h contain the same content, are some string processing functions defined in C language, and string is a class that handles strings defined in C + +, which is completely different from the previous two.
StringStream
The problem of type conversion is often encountered during programming, such as the need to convert 5 of this int-type number to the string "5", and there are some methods in C that can be implemented but not useful. C + + defines a class StringStream, this class object can be very good implementation of type conversion, this class is defined in the header file Sstream, so the application needs to include the header file
#include <sstream>
StringStream SS; Defines object
string str;
ss<<4;
ss>>str;
Once you have finished using the Stringsteam object, clear () clears the previous content before using it again.
Detailed reference: http://blog.csdn.net/xw20084898/article/details/21939811