Summary of usage of the string class in standard C + +

Source: Internet
Author: User

Original address: http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html

Believe that the use of MFC programming friends to CString this kind of impression should be very deep it? Indeed, the CString class in MFC is really easy to use. But if you leave the MFC framework, are there any classes that are easy to use? The answer is yes. Perhaps some people will say, even without the MFC framework, you can find ways to use the MFC API, the specific operation method at the end of this article gives the operation method. In fact, many people may well ignore the use of string classes in standard C + +. The string classes provided in standard C + + are also very powerful, and are generally used to meet our development projects. Now the specific use of the part listed below, only a starting point of the role of it, good, nonsense less, directly into the subject bar!

To use the string class in standard C + +, you must include the

#include <string>//note is <string>, not <string.h>, with. h is the header file in the C language

Using Std::string;

Using Std::wstring;

Or

using namespace Std;

Below you can use the string/wstring, which correspond to char and wchar_t respectively.

The use of string and wstring is the same, the following is only described in string:

constructor of the String class:

String (const char *s); Initialize with C string s
string (int N,char c); Initialize with n characters c
In addition, the string class supports both the default constructor and the copy constructor, such as String s1;string s2= "Hello", which is the correct notation. A Length_error exception is thrown when the constructed string is too long to be expressed;

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 () both return the position of the nth character in the current string, but the at function provides a scope check that throws a Out_of_range exception when it crosses the border, and the subscript operator [] does not provide check access.
const char *data () const;//returns a non-null-terminated C-character array
const char *C_STR () const;//returns a null-terminated C string
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

Character description of String:


int capacity () const; Returns the current capacity (that is, the number of elements in a string that do not have to increase memory)
int max_size () const; Returns the length of the largest string that can be stored in a string object
int size () const; Returns the size of the current string
int length () const; Returns the length of the current string
BOOL empty () const; Whether the current string is empty
void Resize (int len,char c);//Set the current size of the string to Len and fill in the insufficient part with the character C

Input and output operations of the String class:


The string class overloaded operator operator>> is used for input, and the same overloaded operator operator<< is used for output operations.
The function getline (IStream &in,string &s) is used to read the string from the input stream in to s, separated by a newline character ' \ n '.

Assignment of String:


String &operator= (const string &s);//assigns the string s to the current string
String &assign (const char *s);//Assigning a value with the C type string s
String &assign (const char *s,int n);//n characters assigned value starting with C string s
String &assign (const string &s);//assigns the string s to the current string
String &assign (int n,char c);//assigns a value to the current string with n characters c
String &assign (const string &s,int start,int N);//assigned the current string from the n characters in the string s from start
String &assign (Const_iterator first,const_itertor last);//Assigning the part between first and last iterators to a string

Connection to String:


String &operator+= (const string &s);//Connect the string s to the end of the current string
string &append (const char *s);            //Concatenate the C-type string s to the end of the current string
string &append (const char *s,int n);//Connect prompt the first n characters of type C 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 that starts from Pos in the string s connect prompt to the end of the current string
string &append ( int N,char c);       //Add n characters to the end of the current string C
String &append (const_iterator First,const_iterator last);//Connect the part between the iterator first and last to the end of the current string

Comparison of string:
BOOL operator== (const string &s1,const string &s2) const;//compares two strings for equality
The operators ">", "<", ">=", "<=", "! =" are overloaded for string comparisons;
int compare (const string &s) const;//Compare the size of the current string and s
int compare (int pos, int n,const string &s) const;//compares the current string with the size of S of N characters starting from Pos
int compare (int pos, int n,const string &s,int pos2,int n2) const;//Compare the string of n characters that the current string starts from Pos with S

Pos2 the size of a string that starts with N2 characters
int compare (const char *s) const;
int compare (int pos, int n,const char *s) const;
int compare (int pos, int n,const char *s, int pos2) const;
The Compare function returns -1,== when > returns 1,< when return is 0

Substring of string:


String substr (int pos = 0,int n = NPOs) const;//Returns a string of n characters starting at Pos

exchange of String:

void Swap (string &s2); Swap the current string with the value of S2

Lookup function for string class:

int find (char c, int pos = 0) const;//The position of the character C at the current string starting from the POS
int find (const char *s, int pos = 0) const;//Find the position of the string s in the current string starting from the POS
int find (const char *s, int pos, int n) const;//from Pos to find the position of the first n characters in the string s in the current string
int find (const string &s, int pos = 0) const;//Find the position of the string s in the current string starting from the POS
Returns the location when the lookup succeeds, and fails to return the value of String::npos
int RFind (char c, int pos = NPOs) const;//The position of the character C in the current string from the beginning of the POS
int rfind (const char *s, int pos = NPOs) const;
int rfind (const char *s, int pos, int n = npos) const;
int RFind (const string &s,int pos = NPOs) const;
Finds the position of the first n characters in the string s in the current string from the beginning of the POS, and returns the value of String::npos on failure.
int find_first_of (char c, int pos = 0) const;//Find the first occurrence of character C from Pos
int find_first_of (const char *s, int pos = 0) const;
int find_first_of (const char *s, int pos, int n) const;
int find_first_of (const string &s,int pos = 0) const;
Finds the position of the character in the first n-character array of s in the current string starting from Pos. Find failed return String::npos
int find_first_not_of (char c, int pos = 0) const;
int find_first_not_of (const char *s, int pos = 0) const;
int find_first_not_of (const char *s, int pos,int n) const;
int find_first_not_of (const string &s,int pos = 0) const;
Finds the first occurrence of a character not in the string s from the current string, fails back to String::npos
int find_last_of (char c, int pos = NPOs) const;
int find_last_of (const char *s, int pos = NPOs) const;
int find_last_of (const char *s, int pos, int n = npos) const;
int find_last_of (const string &s,int pos = NPOs) const;
int find_last_not_of (char c, int pos = NPOs) const;
int find_last_not_of (const char *s, int pos = NPOs) const;
int find_last_not_of (const char *s, int pos, int n) const;
int find_last_not_of (const string &s,int pos = NPOs) const;
Find_last_of and find_last_not_of are similar to find_first_of and find_first_not_of, except that they look forward from behind.

substitution function for string class:
String &replace (int p0, int n0,const char *s);//delete p0 characters from N0, then insert string s at P0
String &replace (int p0, int n0,const char *s, int n),//delete p0 start n0 characters, then insert first n characters of string s at P0
String &replace (int p0, int n0,const string &s),//delete p0 characters 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 character, and then insert string s at P0 to start n characters from Pos
String &replace (int p0, int n0,int n, char c),//delete n0 characters beginning with p0, and then insert n characters C at p0
String &replace (iterator first0, iterator last0,const char *s);//replace the section between [First0,last0] with the string s
String &replace (iterator first0, iterator last0,const char *s, int n);//replace the section between [First0,last0] with the first n characters of S
String &replace (iterator first0, iterator last0,const string &s);//replace the section between [First0,last0] with string s
String &replace (iterator first0, iterator last0,int N, char c);//replace the section between [First0,last0] with n characters c
String &replace (iterator first0, iterator Last0,const_iterator first, const_iterator last);//Put [First0, LAST0) between the parts replaced by [First,last] between the strings

Insert function for string class:
String &insert (int p0, const char *s);
String &insert (int p0, const char *s, int n);
String &insert (int p0,const string &s);
String &insert (int p0,const string &s, int pos, int n);
First 4 functions Insert the first n characters of the POS in the string s in the P0 position
String &insert (int p0, int n, char c);//This function inserts n characters c at p0
Iterator insert (iterator it, char c);//insert character C at it to return the position of the post-insertion iterator
void Insert (iterator it, Const_iterator first, const_iterator last);//insert characters between [first,last] in it
void Insert (iterator it, int n, char c);//insert n characters c in it

Delete function for string class
Iterator Erase (iterator first, iterator last);//delete all characters between [First,last] and return the position of the post-delete iterator
Iterator Erase (iterator it);//Remove the character that it points to, and return the location of the post-delete iterator
String &erase (int pos = 0, int n = npos);//delete the n characters starting at POS and return the modified string

iterator processing for the string class:

The string class provides a forward and backward traversal of the iterator iterator, which provides the syntax for accessing individual characters, similar to a pointer operation, where the iterator does not check the scope.
The iterator variable is declared with String::iterator or string::const_iterator, and Const_iterator does not allow the content of the iteration to be changed. Common iterator functions are:
Const_iterator begin () const;
Iterator begin (); Returns the starting position of a string
Const_iterator end () const;
Iterator End (); Returns the position after the last character of a string
Const_iterator rbegin () const;
Iterator Rbegin (); Returns the position of the last character of a string
Const_iterator rend () const;
Iterator rend (); Returns the front of the first character position of a string
Rbegin and rend are used for iterative access from backward forward, by setting the iterator String::reverse_iterator,string::const_reverse_iterator implementation

String Stream Processing:


By defining Ostringstream and Istringstream variable implementations, #include <sstream> header files
For example:
String input ("Hello,this is a Test");
Istringstream is (input);
String S1,s2,s3,s4;
Is>>s1>>s2>>s3>>s4;//s1= "Hello,this", s2= "is", s3= "a", s4= "test"
Ostringstream OS;
os<<s1<<s2<<s3<<s4;
Cout<<os.str ();

The above is a brief introduction to the C + + string class. With good words it has the function of the MFC in the CString class less than how much, hehe, personal opinion!

Finally, we'll show you how to reference some of the classes in MFC in the Win32 application, such as CString.

1. Right-click on the project directory to select "Properties"---> "Configuration properties"---> "General"---> ' Use of MFC '---> ' use MFC in a Static Library ",

The default is: "Use standard Windows Libraries", such as:

2. Include # include <afxwin.h> before all the header files you use, for example: You can include the # include <afxwin.h> header file at the very front of the stdafx.h file, so you can use it in your source code

CString class, but this also has a disadvantage, is compiled out of the program is much larger than the original. I tried a small program and chose "Use standard Windows Libraries" to compile it.

Release version of about 92KB, using the "use of MFC in a Static Library" compiled release version of about 192KB, a large 100kb, this is a personal consideration ...

Summary of usage of the string class in standard C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.