Class string parsing

Source: Internet
Author: User
Tags strtok

Reprint please indicate source: http://www.cnblogs.com/shrimp-can/p/5645248.html

When it comes to strings, we can define character arrays or pointers, and there is a class that is specifically designed for strings, that is, the class string, which is contained in the header file <string>, in the namespace STD, so using the string class requires a namespace Std.

First, initialization, assignment of the use of

1. Constructor string (): Used for initialization.

String ();
Default constructor
String (const string& str);
Copy constructor
String (const string& STR, size_t pos, size_t len = NPOs);
Copy part of STR and copy len length starting at Pos
String (const char* s);
Copy the string that s points to
String (const char* s, size_t N);
Copies the first n characters of the string to which s is pointing
String (size_t N, char c);
Fills a string with n characters c
Template <class inputiterator>  string  (inputiterator first, Inputiterator last);
Copy returns the sequence of characters in [first, Las] return
String (initializer_list<char> il);
Copy each character in Il (c++11)
String (string&& str) noexcept;
Get content in Str (C++11)
  std::string s0 ("Initial string");  std::string s1;  std::string s2 (s0);  std::string s3 (s0, 8, 3);  std::string s4 ("A character sequence", 6);  std::string s5 ("Another character sequence");  ‘x‘);  std::string s6b (10, 42);      // 42 is the ASCII code for ‘*‘  std::string s7 (s0.begin(), s0.begin()+7);  "s1: ""\ns2: ""\ns3: " << s3;  "\ns4: ""\ns5: ""\ns6a: " << s6a;  "\ns6b: ""\ns7: "‘\n‘;

Output:

s1: s2: Initial strings3: strs4: A chars5: Another character sequences6a: xxxxxxxxxxs6b: **********s7: Initial

2.= Operation :string& operator= (const string& str);

string& operator= (const char* s);

string& operator= (char c);

string& operator= (initializer_list<char> il); (c++11)

string& operator= (string&& str) noexcept; (c++11)

Assigns a value that returns the this pointer.

Second, the iterator correlation function

1.begin: Iterator begin () noexcept;

Const_iterator begin () const noexcept;

Returns an iterator that points to the first character of a string

2.end: Iterator end () noexcept;

Const_iterator End () const noexcept;

Returns an iterator that points to a character after the last character of the string, typically used with begin, to iterate through each character in a string

3.rbegin: Reverse_iterator rbegin () noexcept;

Const_reverse_iterator rbegin () const noexcept;

Returns an iterator that points to the last character of the string

4.rend: reverse_iterator rend () noexcept;

Const_reverse_iterator rend () const noexcept;

Returns an iterator to the previous theoretical element of the first character of the string, typically used with rbegin, to iterate through each character in the string

std::string str ("now step live...");for (std::string::reverse_iterator rit=str.rbegin(); rit!=str.rend(); ++rit)    std::cout << *rit;

Output: ... evil pets won

5.cbegin:const_iterator cbegin () const noexcept; (c++11)

Returns an iterator to the first character of a string, similar to begin, but cannot update content

6.cend:const_iterator cend () const noexcept; (c++11)

Returns an iterator that points to a character after the last character of the string, typically used with cbegin, to iterate through each character in the string. Same as end, but cannot be used to update content

7.crbegin:const_reverse_iterator crbegin () const noexcept; (c++11)

Returns an iterator that points to the last character of the string

8.crend:const_reverse_iterator crend () const noexcept; (c++11)

Returns an iterator to the previous theoretical element of the first character of the string, typically used with crbegin, to iterate through each character in the string

Three, capacity-related functions:

1.size: size_t size () const;

Returns the length of the string, that is, how much bytes

2.length: size_t length () const;

Returns the length of the string, that is, how many bytes, similar to size

3.max_size: size_t max_size () const;

Returns the maximum length that a string can reach

4.resize: void resize (size_t n);

void Resize (size_t n, char c);

Sets the length of the string to n characters, and if the second argument has one, fills the newly added character space with the character C

5.capacity: size_t capacity () const;

Returns the storage space currently assigned to a string, how many bytes

6.reserve: void Reserve (size_t n = 0)

Request string capacity to accommodate the length of the string to be changed to n characters. If n is larger than the current capacity, it is incremented by capacity to n characters, otherwise it does not change.

7.clear: void Clear ();

Clears the contents of a string so that it becomes an empty string with a length of 0 characters

8.empty: BOOL Empty () const;

Returns whether the string is empty, that is, whether its length is 0, or False if it returns true

9.shrink_to_fit: void Shrink_to_fit ();

Request a string to reduce capacity to fit its size.

Iv. element Access functions:

1.[] Operation :char& operator[] (size_t POS);

Const char& operator[] (size_t pos) const;

Returns the character of the position Pos, in the form of an array

2.at:char& at (size_t POS);

Const char& at (size_t pos) const;

Returns the character at position Pos

3.back:char& back ();

Const char& back () const;

Returns a reference to the last character of a string

4.front:char& Front ();

Const char& Front () const;

Returns a reference to the first character in a string

V. Modifying the relevant functions

1.+= Operation :string& operator+= (const string& str);

string& operator+= (const char* s);

string& operator+= (char c);

string& operator+= (initializer_list<char> il); (c++11)

Adds a string or character to the end of a string.

2.append:string& append (const string& str);

string& Append (const string& str, size_t subpos, size_t Sublen);

string& Append (const char* s);

string& Append (const char* s, size_t N);

string& Append (size_t N, char c);

Template <class inputiterator> string& append (inputiterator First, Inputiterator last);

string& Append (initializer_list<char> il); (c++11)

Add a string or character to the end of a string

3.push_back: void Push_back (char c);

Add a character C at the end of a string

4.assign:string& Assign (const string& str);

string& Assign (const string& STR, size_t subpos, size_t Sublen);

string& Assign (const char* s);

string& Assign (const char* s, size_t N);

string& Assign (size_t N, char c);

Template <class inputiterator> string& assign (inputiterator first, Inputiterator last); (c++11)

string& Assign (initializer_list<char> il); (c++11)

string& Assign (string&& str) noexcept; (c++11)

Replaces the current string content with a new string

5.insert:string& Insert (size_t pos, const string& str);

string& Insert (size_t pos, const string& STR, size_t subpos, size_t Sublen);

string& Insert (size_t POS, const char* s);

string& Insert (size_t POS, const char* S, size_t N);

string& Insert (size_t pos, size_t N, char c);

void Insert (iterator p, size_t N, char c);

Iterator Insert (Const_iterator p, size_t N, char c); (c++11)

Iterator Insert (Const_iterator p, char c);

Template <class inputiterator> iterator insert (iterator p, Inputiterator first, Inputiterator last); (c++11 )

Template <class inputiterator> void Insert (iterator p, Inputiterator first, Inputiterator last);

string& Insert (const_iterator p, initializer_list<char> il); (c++11)

string& Insert (string&& str) noexcept; (c++11)

Inserting a string at position pos or P

6.erase:string& Erase (size_t pos = 0, size_t len = NPOs);

Iterator Erase (iterator p);

Iterator Erase (iterator first, iterator last);

Erases characters or strings, reducing the length.

7.replace:string& replace (size_t pos, size_t len, const string& str);

string& replace (iterator I1, iterator i2, const string& str);

string& Replace (size_t pos, size_t len, const string& STR, size_t subpos, size_t Sublen);

string& Replace (size_t pos, size_t len, const char* s);

string& replace (iterator I1, iterator I2, const char* s);

string& Replace (size_t pos, size_t len, const char* S, size_t N);

string& replace (iterator I1, iterator I2, const char* S, size_t N);

string& Replace (size_t pos, size_t len, size_t N, char c);

string& replace (iterator I1, iterator I2, size_t N, char c);

Template <class inputiterator> string& replace (iterator I1, iterator I2, Inputiterator first, Inputiterato R last);

string& Replace (const_iterator i1, const_iterator i2, initializer_list<char> il); (c++11)

8.swap: void swap (string& str);

Swap the contents of two strings

9.pop_back: void Pop_back ();

Clears the last element in a string

Six, String operations:

1.c_str: const char* C_STR () const;

Returns a pointer to a string content

  std::string str ("Please split this sentence into tokens");  charnewchar [str.length()+1];  std::strcpy (cstr, str.c_str());  char * p = std::strtok (cstr," ");  while (p!=0)  {    ‘\n‘;    p = std::strtok(NULL," ");  }  delete[] cstr;

Output:

Pleasesplitthissentenceintotokens

2.data: const char* data () const;

Returns a pointer to the string content, similar to the C_STR () function

3.get_allocator: Allocator_type get_allocator () const;

Returns a copy of a string-related allocator

4.copy: size_t copy (char* s, size_t len, size_t pos = 0) const;

Copies the Len characters starting at the POS position in string to the array that s points to

5.find: size_t Find (const string& str, size_t pos = 0) const;

size_t Find (const char* s, size_t pos = 0) const;

size_t Find (const char* s, size_t pos, size_t N) const;

size_t Find (char c, size_t pos = 0) const;

Searches for characters or substrings from the POS, returns the position of the first match, and returns String::npos if the search is not found

6.rfind: size_t rfind (const string& str, size_t pos = npos) const;

size_t RFind (const char* s, size_t pos = npos) const;

size_t RFind (const char* S, size_t pos, size_t N) const;

size_t RFind (char c, size_t pos = npos) const;

Searches for a character or substring in front of the POS, returns the position of the last matched first character, and returns String::npos if the search is not found

7.find_first_of: Usage is the same as find () usage

Searches for any character in a parameter character or substring from the POS, returns the position of the first match, or returns String::npos if the search is not found

8.find_last_of: Usage is the same as RFind () usage

Searches for any character in the parameter character or substring before the POS, returns the position of the last matched character, or returns String::npos if the search is not found

9.find_first_not_of: Usage is the same as find ()

Searches for characters from the POS that do not match any characters in the parameter or substring, returns the position of the first unmatched character, and if all matches, returns String::npos

10.find_last_not_of: Usage is the same as RFind () usage

Searches for characters in front of the POS that do not match any characters in the parameter or substring, returns the position of the last unmatched character, and if all matches, returns String::npos

11.substr: String substr (size_t pos = 0, size_t len = npos) const;

Returns a substring at the beginning of a Len character at Pos

12.compare: int compare (const string& str) const;

int compare (size_t pos, size_t len, const string& str) const;

int compare (size_t pos, size_t len, const string& STR, size_t subpos, size_t sublen) const;

int compare (const char* s) const;

int compare (size_t pos, size_t len, const char* s) const;

int compare (size_t pos, size_t len, const char* S, size_t n) const;

Compares two strings, or returns a positive number if the first unmatched character returns a negative number if equal returns 0

Seven, non-member functions, but the string object is often used

1.+ operation

2. Relational operators

3.>>, <<

4.swap: void Swap (string& x, string& y);

5.getline:istream& getline (istream& is, string& str);

Class string parsing

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.