Use of string in C + +

Source: Internet
Author: User

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 a 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 '.

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

string Connection:
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),//The first n characters of type C string s connect prompt to the end of the current string
string &append (const string &s); nbsp;  //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  

 

string的比较:
bool operator==(const string &s1,const string &s2)const;//比较两个字符串是否相等
运算符">","<",">=","<=","!="均被重载用于字符串的比较;
int compare(const string &s) const;//比较当前字符串和s的大小
int compare(int pos, int n,const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小
int compare(int pos, int n,const string &s,int pos2,int n2)const;//比较当前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符组成的字符串的大小
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;
compare函数在>时返回1,<时返回-1,==时返回0 

string的子串:
string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串


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;//从pos开始查找字符c在当前字符串的位置
int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
//查找成功时返回所在位置,失败返回string::npos的值

int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置
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;
//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值

int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置
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;
//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回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;
//从当前串中查找第一个不在串s中的字符出现的位置,失败返回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和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找

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);
//前4个函数在p0位置插入字符串s中pos开始的前n个字符
string &insert(int p0, int n, char c);//此函数在p0处插入n个字符c
iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置
void insert(iterator it, const_iterator first, const_iterator last);//在it处插入[first,last)之间的字符
void insert(iterator it, int n, char c);//在it处插入n个字符c
 

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

The erase function is prototyped as follows:
(1) string& erase (size_t pos = 0, size_t n = NPOs);
(2) Iterator erase (iterator position);
(3) Iterator erase (iterator first, iterator last);
That means there are three ways to use:
(1) Erase (pos,n); Deleting n characters starting from POS, such as erase (0,1), deletes the first character
(2) Erase (position); Deletes a character at position (position is a string type of iterator)
(3) Erase (first,last); Delete the characters from first to last (both first and last are iterators)
Let me give you an example:

#include <iostream>
#include <string>
using namespace std;

int main ()
{
string Str ("This was an example phrase.");
String::iterator it;

//(1) Usage
str.erase (10,8);
cout << str << Endl; "This was an phrase."

//(2) Usage
It=str.begin (+9);
Str.erase (it);
cout << str << Endl; "This is a phrase."

//(3) Usage
str.erase (Str.begin () +5, Str.end () -7);
cout << str << Endl; "This phrase."
return 0;
}

Iterator processing of the string class:

string class provides an iterator iterator for forward and backward traversal, which provides syntax for accessing individual characters, similar to pointer operations, Iterators do not check scopes. The
declares an iterator variable 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 string
Const_iterator end () const;
Iterator End ();                    //Returns the position after the last character of the string
Const_iterator rbegin () const;
Iterator Rbegin ();               // Returns the position of the last character of String
Const_iterator rend () const;
Iterator rend ();                    //Returns the first character position of string before
Rbegin and rend are used to access from backward forward iterations by setting the iterator string::reverse_iterator,string: : Const_reverse_iterator implements

String Stream Processing:

Implemented by defining Ostringstream and Istringstream variables in the,<sstream> header file
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 ();

Use of string in C + +

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.