Summary of usage of string classes in standard C + + __c++

Source: Internet
Author: User
Tags int size

I believe that the use of MFC programming friends to CString this kind of impression should be very profound. Indeed, the CString class in MFC is really convenient and easy to use. But if you leave the MFC framework, there are no classes that are easy to use. The answer is yes. Some people may say that even without the MFC framework, you can find ways to use the API in MFC, 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 class provided in standard C + + is also very powerful and can generally be used when we develop projects. Now the specific use of a part of the list below, only to play a role in the bar, well, less nonsense, directly to the point of it.

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

Summary of usage of string classes in standard C + +

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

#include <string>//attention is <string>, not <string.h>, with. h is the head file
using  std::string
in C language; Using  std::wstring;

Or

using namespace Std;

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

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


constructor of String class:

String (const char *s);    Initializes a string with the C string s
(int n,char c);     Class with N-character C
In addition, the string class also supports default constructors and copy constructors, 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 operations for string classes:

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 range check and throws a Out_of_range exception when the bounds are crossed, 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 n characters in the current string starting with Pos to an array of characters starting with S, returning the number of actual copies

description of the attribute of string:
int capacity () const;    Returns the current capacity (that is, the number of elements in string that do not need to add memory)
int max_size () const;    Returns the length
int size () const of the maximum string that can be stored in a string object;        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 an empty
void resize (int len,char c);//The current size of the string is set to Len, and the insufficient part is populated with character C

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

Assignment of string:

String &operator= (const string &s);//Assign the string s to the current string
&assign (const char *s);//Assign Value
with C type string s String &assign (const char *s,int n);///////= n-word assignments Value string &assign (const string &s) starting with C string s;//
assign string s to current string
string &assign (int n,char c);//////n character C assigned to the current string
&assign (const string &s,int start,int N); Assignments The n characters from start in the string s to the current string
&assign (Const_iterator first,const_itertor last); Assign a section between the primary and last iterators to a string

String's connection:

String &operator+= (const string &s);//Connect the string s to the end of the current string
&append (const char *s);            Concatenate the C type string s to the end of the current string
&append (const char *s,int n);//The first n characters of the C type string s are fonts to the end of the current string
&append ( const string &s);    With operator+= ()
string &append (const string &s,int pos,int N);//fonts to the end of the current string with n words from the pos in string s
&append (int n,char c);        Add n characters at the end of the current string C
string &append (Const_iterator first,const_iterator last); Connect the portion of the iterator between the primary and last to the end of the current string
Comparison of string:
BOOL operator== (const string &s1,const string &s2) const;//compare two strings for equality
Operators ">", "<", ">=", "<=", "!=" are overloaded for string comparisons;
Compare function returns 0 when 1,< returns -1,== at >


Substring of string:
String substr (int pos = 0,int n = NPOs) const;//return of N-character strings starting with Pos

Exchange of String:
void Swap (string &s2); Swap the value of the current string and S2


Lookup function for string class:

int find (char c, int pos = 0) const;//Look for the character C at the position of the current string int found (const char *s, int pos = 0) const;//Look up the string s in the current string from POS Position int Find (const char *s, int pos, int n) const;//look up the position of the first n characters in the string s in the current string by starting with the POS int found (const string &s, int pos = 0) C onst;//from Pos to find the position of string s in the current string//Find the location of success, return the value of String::npos int rfind (char c, int pos = NPOs) const;//
Look up the character C in the current string from the point of 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; From the beginning of the POS to find the position of the first n characters in the string s in the current string, successfully return to the location, and return string::npos value int find_first_of (char c, int pos = 0) const;//
Starting from Pos Find the first occurrence of the character c the position 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; Starts from the POS to find the position of the first character in the current string in an array of the top n characters of S.
Lookup failure Returns 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 string s from the current string, and the failure returns 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, just looking forward from the back

substitution function for string class:

String &replace (int p0, int n0,const char *s)//delete p0 characters starting from N0, then insert string s string p0 at &replace (int p0, int n0,const Char *s, int n);//delete p0 start N0 characters, and then insert the first n characters of string s at p0 (int &replace, int p0 string n0,const);//delete &s characters starting from P0, Then insert the string s string &replace (int p0, int n0,const string &s, int pos, int n) at P0 Place,//delete p0 characters beginning N0, then insert n characters from Pos in string s at P0 Tring &replace (int p0, int n0,int n, char c);//delete p0 characters starting N0, and then insert N-character C string p0 at &replace (iterator first0, Iterat or Last0,const char *s),//replace the portion between [first0,last0) with the string ' s string &replace (iterator first0, iterator Last0,const Char *s, int n);//replace the portion between [first0,last0) with the first n characters string &replace of S (iterator first0, iterator last0,const string &s); FIRST0,LAST0) is replaced by the string s string &replace (iterator first0, iterator last0,int N, char c);/[First0, LAST0) is replaced by the N-character C string &replace (iterator first0, iterator Last0,const_iterator first, and Const_iterator last); [First0,last0) replaced by a string between [First,last]

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);
The first 4 functions insert the first n characters string
&insert (int p0, int n, char c) at the beginning of the POS in the p0 position;//This function inserts n characters c
iterator insert at P0 Iterator it, char c);//inserts character C at it, returns the position of the inserted iterator,
void Insert (iterator it, Const_iterator, Const_iterator last); Insert the character
void insert (iterator it, int n, char c) between the [First,last) at it;//insert n characters C at it

Delete function for string class

Iterator Erase (iterator primary, iterator last);//delete all characters between [First,last), returning the location of the post-deletion iterator
iterator erase (iterator it); /Remove the character that it points to, return the position of the post-deletion iterator
string &erase (int pos = 0, int n = npos);//delete the N-character at the beginning of the POS, return the modified string


iterator Processing for string classes:
The string class provides iterator iterator for forward and backward traversal, which provides the syntax to access individual characters, similar to pointer operations, without the iterator checking the scope.
Declaring an iterator variable with string::iterator or String::const_iterator, Const_iterator does not allow changes to the contents of the iteration. Common iterator functions are:
Const_iterator begin () const;
Iterator begin ();                Returns the starting position of 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 rbegin and rend of the first character position of string
used to iterate from the back 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. The use of good words it has the function will not be less than MFC CString class inferior how much, hehe, personal opinion.

Finally, describe how to reference some classes in MFC, such as CString, in an WIN32 application.

1. In the Engineering directory, right-click Select "Properties"---> "Configuration properties"---> "General"---> "Use of MFC"---> "Use MFC in a Static Library ",

The default is: "Use Standard Windows Libraries", as shown in the following figure:

      

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

CString class, but there is also a drawback, that is, compiled the program is much larger than the original. I have tried a small program, choose "Use Standard Windows Libraries" compiled

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

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.