1String Concept
String is a string type of STL that is commonly used to represent strings. Before using string, the string is usually represented by char*. Both string and char* can be used to represent strings, so what is the difference between them?
Comparison of String and char*
A string is a class that char* is a pointer to a character.
String encapsulates the char*, which manages the string and is a char* type container.
The string does not consider memory deallocation and out-of-bounds.
String manages the memory allocated by the char*. Each time a string is copied, the value is maintained by the string class, without worrying about copying out-of-bounds and value out of bounds.
String provides a series of string manipulation functions (this will be discussed in more detail)
Find found, copy copy, delete erase, replace replace, insertConstructors for 2string
Default constructor:
String (); Constructs an empty string S1.
Copy constructor:
String (const string &str); Constructs a string that is the same as Str. such as String S1 (S2).
Constructors with parameters
String (const char *s); Class with the string S.
string (int N,char c); Initialize with n characters of C3string Access character operation
Character manipulation of the string class:
const char &operator[] (int n) const;
const char &at (int n) const;
Char &operator[] (int n);
char &at (int n);
Operator[] and at () all return the nth character in the current string, but the two are different.
The main difference is that the at () throws an exception when it crosses the boundary, [] it returns (char) 0 when it crosses the border, and the compiler makes an error when it goes out of bounds. If your program wants to catch an exception through Try,catch, it is recommended to use at (). 4 getting the const char* from string
const char *C_STR () const; Returns the first address of a string ending with '% '5 Copying a string to an operation that char* points to a memory space
int copy (char *s, int n, int pos=0) const;
Copies the n characters starting at Pos in the current string to a character array starting with S, returning the number of actual copies. Note that the space pointed to by S is large enough to accommodate the current string, otherwise it will go out of bounds. Length of 6string
int length () const; Returns the length of the current string. Length does not include ' + ' at the end of the string.
BOOL empty () const; Whether the current string is emptyAssignment of 7string
String &operator= (const string &s);//assigns the string s to the current string
String &assign (const char *s); Assigns the string s to the current string
String &assign (const char *s, int n); Assigned the first n characters of the string s to the current string
String &assign (const string &s); Assigns the string s to the current string
String &assign (int n,char c); Assigns the current string with n characters c
String &assign (const string &s,int start, int n); Assigned n characters from start in string s to the current string8string String Connection
String &operator+= (const string &s); Concatenate the string s to the end of the current string
String &operator+= (const char *s);//Connect the string s to the end of the current string
String &append (const char *s); Concatenate the string s to the end of the current string
String &append (const char *s,int n); Connect prompt the first n characters of the string s to the end of the current string
String &append (const string &s); With operator+= ()
String &append (const string &s,int pos, int n);//The N-word from POS starting in string s connect prompt to the end of the current string
string &append (int n, char c); Adds n characters to the end of the current string CComparison of 9string
int compare (const string &s) const; Compare to String s
int compare (const char *s) const; Compare to String s
The Compare function returns 0 when 1,< returns -1,== when > returns. Comparisons are case-sensitive, with reference to the dictionary order, and the smaller the front row. Uppercase A is smaller than lowercase a. Sub-string of 10string
string substr (int pos=0, int n=npos) const; Returns a substring of n characters starting at the POS
11string Find and replace
Find
int find (char c,int pos=0) const; Find character C at position of current string starting from Pos
int find (const char *s, int pos=0) const; Finds the string s position in the current string starting from the POS
int find (const string &s, int pos=0) const; Find the position of the string s in the current string starting from the POS
The Find function returns 1 if it is not found
int RFind (char c, int pos=npos) const; The position of the character C in the current string from the start of the POS from the back forward
int rfind (const char *s, int pos=npos) const;
int RFind (const string &s, int pos=npos) const;
RFind is the meaning of reverse lookup, if not found, returns-1
Replace
String &replace (int pos, int n, const char *s),//delete n characters starting from Pos, then insert string s at POS
String &replace (int pos, int n, const string &s); Delete n characters starting at Pos, then insert string s at POS
void Swap (string &s2); Swap the current string with the value of S2
12String interval Delete and insert
String &insert (int pos, const char *s);
String &insert (int pos, const string &s);
First two functions insert string s at POS location
String &insert (int pos, int n, char c); Insert n characters c at POS location
String &erase (int pos=0, int n=npos); Deletes the n characters starting at POS and returns the modified string
Demo
#include <iostream> #include <cstdio> #include <string> #include <algorithm>using namespace std ;//initialization of the string void Initstring () {cout << "init of String object\n"; string s1 = "Lucifer"; string S2 ("Zhang"); string S3 = S2; Initialize the object by copying the constructor s3string S4 (' a '), cout << S1: << S1 << endl;cout << "s2:" << S2 < < Endl;cout << "S3:" << S3 << endl;cout << "S4:" << S4 << endl;cout << Endl ;} Traverse stringvoid ergodic () {cout << "ergodic of String object\n"; string s = "Lucifer";//1 array mode for (unsigned int i = 0; I < s.length (); i++) {cout << s[i] << ';} cout << endl;//2 iterator mode for (string::iterator it = S.begin (); It! = S.end (); it++) {cout << *it << ';} cout << endl;//3at () method try{for (unsigned int i = 0; i < s.length () + 3; i++) {cout << s.at (i) << '; Throw exception}cout << Endl;} catch (...) {cout << "exception occurred \ n";} cout << endl;// If subscript is used, the program aborts the/*try{for (unsigned int i = 0; i < s.length () + 3; i++) {cout << s[i] << ";} cout << Endl;} catch (...) {}*/}//character pointer and string conversion void Char2string () {string s = "Lucifer";//String--Char*cout << s.c_str () << endl;/ /string Content Copy,buf char buf[128] = {0};s.copy (buf, 3, 0); Note that only copy3 characters will not become a C-style string cout << "buf:" << buf << endl;cout << Endl;} string connection void Stringappend () {string S1 = "Lucifer"; string s2 = "Zhang"; s1 = s1 + s2;cout << "S1:" << S1 & lt;< endl;string s3 = "333"; string s4 = "444"; S3.append (S4) cout << "S3:" << S3 << Endl;cout << ; Endl;} string find and replace void Stringfindandreplace () {string s = "Lucifer Hello Lucifer 111 Lucifer 222 Lucifer 333"; int index = S.fin D ("Lucifer", 0); From the location subscript 0 start looking for cout << "index:" << index << endl;//Find out the number of occurrences of Lucifer, each occurrence of the array subscript int offindex = S.find ("Lucif Er ", 0); while (offindex! = string::npos) {cout <<" off index: " << offindex << endl;offindex = S.find ("Lucifer", ++offindex);} Replace the Lucifer in s with uppercase Offindex = S.find ("Lucifer", 0), while (offindex! = string::npos) {cout << "off index:" << Offindex << endl;s.replace (Offindex, 7, "LUCIFER"); Offindex = S.find ("LUCIFER", ++offindex);} cout << ' after replace: ' << s << endl;cout << endl;//after replace:lucifer Hello LUCIFER 111 LU Cifer 222 LUCIFER 333}//truncation (interval delete) and insert void Stringdelete () {string s = "Hello LUCIFER hello"; string::iterator it = find (S.begi N (), S.end (), ' l '); if (it = S.end ()) {s.erase (it);} cout << "After delete ' L ':" << s << endl;//after delete ' l ': helo Lucifer Hellos.erase (S.begin (), S.en D ()); Delete all cout << ' after delete all: ' << s << endl;//after delete all:string s2 = "Zhang"; S2.insert (0, "l Ucifer "); Head interpolation cout << "after insert:" << s2 << endl;//after Insert:luciferzhangs2.insert (S2.length (), "Hello" ); Tail interpolation cout << "AFter insert: "<< s2 << endl;//after insert:luciferzhanghellocout << Endl;} void Stringtransform () {string s = "Luciferzhang"; transform (S.begin (), S.end (), S.begin (), toupper); cout << "s:" &L t;< s << endl;//s:luciferzhangtransform (S.begin (), S.end (), S.begin (), tolower); cout << "s:" << s << endl;//s:luciferzhangcout << Endl;} int main () {initstring (); ergodic (); char2string (); Stringappend (); Stringfindandreplace (); Stringdelete (); Stringtransform (); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Stl-string (typical operation demo)