C + + Learning: string usage

Source: Internet
Author: User

Function

Function prototypes

Description

constructor function

String (const char *s);

Class with C-style string S.

string (int N,char c);

Initialize with n characters c

Default constructor

Copy constructor

accessing elements

const char &operator[] (int n) const;

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.

Char &operator[] (int n);

const char &at (int n) const;

char &at (int n);

C-style string manipulation

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

Feature description

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 portion with the character C

Input/Output

Overloaded operator operator>>

For input

Overloaded operator operator<<

For output

Getline (IStream &in,string &s);

Used to read the string from the input stream in to s, separated by the newline character ' \ n '.

Assign value

String &operator= (const string &s);

Assigns the string s to the current string

String &assign (const char *s);

Assign a value to 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 n characters from start in string s to the current string

String &assign (Const_iterator first,const_itertor last);

Assigns the part between first and last iterators to a string

Connection

String &operator+= (const string &s);

Concatenate 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);

Connect prompt the n characters from the POS in the string s to the end of the current string

String &append (int n,char c);

Adds 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

BOOL operator== (const string &s1,const string &s2) const;

Compare two strings for equality, operator ">", "<", ">=", "<=", "! =" 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 size of a string of n characters starting with the current string from POS to s

int compare (int pos, int n,const string &s,int pos2,int n2) const;

Compares the size of a string of n characters starting at the beginning of the current string from Pos with N2 characters starting with Pos2 in S

int compare (const char *s) const;

Compared to a C-style string. The Compare function returns -1,== when > returns 1,< when return is 0

int compare (int pos, int n,const char *s) const;

int compare (int pos, int n,const char *s, int pos2) const;

Sub-string

String substr (int pos = 0,int n = npos) const;

Returns a string of n characters starting at Pos

Exchange

void Swap (string &s2);

Swap the current string with the value of S2

Find

int find (char c, int pos = 0) const;

Finds the character C from the Pos at the position of the current string, returns the location when the lookup succeeds, and fails to return the value of String::npos

int find (const char *s, int pos = 0) const;

Finds 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 find (const char *s, int pos, int n) const;

Finds the position of the first n characters of the string s in the current string from the point of POS, returns the location when the lookup succeeds, and fails to return the value of String::npos

int find (const string &s, int pos = 0) const;

Finds 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, starting from the POS and looking forward from behind

int rfind (const char *s, int pos = NPOs) const;

Returns the position of the string s in the current string from the start of the POS, back to the position where it was successfully returned, returning the value of String::npos on failure

int rfind (const char *s, int pos = NPOs, int n) 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 RFind (const string &s,int pos = NPOs) const;

Returns the position of the string s in the current string from the start of the POS, back to the position where it was successfully returned, returning the value of String::npos on failure

int find_first_of (char c, int pos = 0) const;

Find the first occurrence of the character C starting from Pos

int find_first_of (const char *s, int pos = 0) const;

Finds the position of the first s in the current string, starting from the Pos. Find failed return String::npos

int find_first_of (const char *s, int pos, int n) 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_of (const string &s,int pos = 0) const;

Finds the position of the first s in the current string, starting from the Pos. Find failed return String::npos

int find_first_not_of (char c, int pos = 0) const;

Finds the first position not appearing in the current string where the character C appears from the POS, and fails back to String::npos

int find_first_not_of (const char *s, int pos = 0) const;

Finds the first position in the string s that does not appear in the current string, starting with the POS, and fails back to String::npos

int find_first_not_of (const char *s, int pos,int n) const;

Finds the position of the first n-character string that does not appear in the current string, starting from the POS, and fails back to String::npos

int find_first_not_of (const string &s,int pos = 0) const;

Finds the first position in the string s that does not appear in the current string, starting with the POS, and fails back to String::npos

int find_last_of (char c, 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.

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;

Replace

String &replace (int p0, int n0,const char *s);

Delete N0 characters starting with P0 and insert string s at P0

String &replace (int p0, int n0,const char *s, int n);

Remove N0 characters starting with P0, and then insert the first n characters of the string s at P0

String &replace (int p0, int n0,const string &s);

Delete N0 characters starting with P0 and insert string s at P0

String &replace (int p0, int n0,const string &s, int pos, int n);

Delete N0 characters starting with P0 and then insert string s n characters from Pos at P0

String &replace (int p0, int n0,int n, char c);

Remove N0 characters starting with P0, and then insert n characters at P0 c

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 a 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);

Replace the part between [First0,last0] with a string between [First,last]

Insert

String &insert (int p0, const char *s);

Inserts the first n characters of the POS starting in the P0 position in the string 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);

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 the characters between [First,last] in the IT department

void Insert (iterator it, int n, char c);

Insert n characters c in it

Delete

string& Erase (size_t pos = 0, size_t n = NPOs);

Delete n characters starting from POS, return modified string

Iterator Erase (iterator it);

Remove the character that it points to, and return the location of the post-delete iterator

Iterator Erase (iterator first, iterator last);

Delete the characters from first to last (both first and last are iterators), returning the position of the post-delete iterator

Iterators

Const_iterator begin () const;

Returns the starting position of the string. To declare an iterator variable with string::iterator or String::const_iterator, Const_iterator does not allow the content of the iteration to be changed

Iterator begin ();

Const_iterator end () const;

Returns the position after the last character of a string

Iterator End ();

Const_iterator rbegin () const;

Returns the position of the last character 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

Iterator Rbegin ();

Const_iterator rend () const;

Returns the position in front of the first character of a string

Iterator rend ();

Stream processing

By defining Ostringstream and Istringstream variable implementations, #include <sstream> header files

C + + Learning: string usage

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.