C ++ String summarizes the main components of the STL (the first portion, the construction and operation)

In a recent study C ++, into the STL learning phase, when the discovery of the importance of this part, I intend to learn of the STL step by step record, first I intend to learn that some of the components of String, this article is mainly only basic structure and internal recording operation.

STL is provided by the C ++ standard template library, containing a number of major components, the summary of the contents of String section. String considered a more important part in the STL, so I need to focus overcome.

I first put a generalization after learning String for this part of the knowledge points.
C ++ String summarizes the main components of the STL (the first portion, the construction and operation)

The first is the first part:

A standard library String class has what

First is String included Interface:
In fact, this part is not difficult to understand, because most had more or less written before the functionality of these functions.
1. Constructor
(1) no-argument constructor
string () // empty string object configuration, an empty string
eg:

string s1;//构造空的String类对象s1

(2) parameterized constructor
case common to more than one of the constructor
① c style parameter string
String (const char * S)
EG:

string s2(“hello”);//使用C风格字符串构造的String类对象

② target configuration contains n characters C
String (size_t n, char C)
EG:

string s3(10,‘n’);//构造的对象为字符串“nnnnnnnnnn”

③ copy constructor (the object of the same class use the construction of new class object)
String (const String & S)
EG:
string s4(s3);

2.String class object for the operating capacity of
this portion in fact is actually going all the learning efficiency is not high, as shown below
C ++ String summarizes the main components of the STL (the first portion, the construction and operation)
(Source cplusplus.com)
so the main selected more frequently using several (size, empty, clear, reserve , resize) to key learning can be.
(. 1) size
size function returns the current object is a valid character length
eg:

string s;
cout << s.size();

(2) empty
empty detects whether the current string is empty string has been released, it returns a true, whether it returned to false
EG:

string s;
if(s.empty()){
return 0;
}

(3) the Clear
the Clear will clear the current valid character object, but does not change the underlying space
eg:

string s;
s.clear();

(. 4) a resize (n-size_t, char c)
a resize modify the effective number of characters in the current object, the extra portion of the character will be used to fill c
before and modified if the character length is less than effective, then the excess portion will be truncated
eg:

string s;
s.resize(20,'w');


Note that points are:

<1> If the string class reduced effective element, varying the effective number of elements only, without changing the underlying space
<2> class if the increase in the effective element string may require expansion (changes in the underlying space)

(. 5) Reserve (size_t newCapacity)
Reserve modify the current object underlying space, but does not alter the number of valid characters
when newcapacity> when oldcapacity (string class old space), for expansion
if newcapacity <When oldcapacity, the function returns immediately no modifications
eg:

string s;
reserve(20);

Need some attention:
in different contexts, Reserve size-expansion is not the same (when the object 15 is less than the string length, the initial allocation as the bottom space 15):
for example: In the internet expansion vs original size is approximately 1.5
However, expansion in the size of the original is twice linux

Finally, the small capacity operation points:
size () with a length () method to achieve exactly the same underlying principles, incorporated size () is the reason for consistency with other container interface (function return values are identical)

3. For the string class object access method
(1) access to the specified position of
an object operation, such access is generally used operator [] is performed (similar to the access operation of array elements)
EG:

string s(“hello”);
cout << s[0];

Output:
H
(2) using traversal methods to access (or modified)
in C ++, here we can use a total of traversal methods. 3 (PS:! IT like the I O (∩ _ ∩) O)
<. 1> using a for loop plus operator [] to traverse a string
eg:

string s(“beat it”);
for(size_t i=0;i<=s.size();++i)
cout << s[ i ];

<2> a method iterators to traverse
Note:
① library container may be used iterator
②begin () method is responsible for returning an iterator of the first element, end () method is responsible for returning a position point to the last element of the container iterator
③ If the container is empty, the begin and end in the same return an iterator
④rbegin () returns a reverse iterator to the last character string, rend () function returns a reverse iterator to the first the previous position of the character, the specific location for the following:
C ++ String summarizes the main components of the STL (the first portion, the construction and operation)
(Photo from Internet)

eg:

string::iterator it = s.begin();  
 while(it != s.end())
 {
 cout<<*it<<endl;
 ++it;       //指向元素位置移动如上图
 }

 string::reverse_iterator rit = s.rbegin(); //此处为反向迭代器的使用
 while(rit != s.rend())
 {
 cout<<*rit<<endl;
++rit;     //指向元素位置移动如上图
}

<3> Method for traversing range
eg:

for(auto c : s)
cout << c << endl;

4. Modify operation on the content of the string class object

string type commonly used in such operations are summarized as follows:

  • push_back - c is inserted after the end of the string of characters
    eg:
    string s1;
    s1.push_back('I');
  • append - append a string after the string (there are many ways to use, below)
    C ++ String summarizes the main components of the STL (the first portion, the construction and operation)
    EG:
    Two commonly used:
    s1.append("祖国");   //图中第一种
    s1.append(3, '!');    //图中第五种
  • operator + = - appending the string after the string str
    eg:
    s1 += " Love ";
  • c_str - Returns the format string C
  • find + npos - from a string of characters to find the position pos onward c, to return the character position in the string
    eg:

    size_t pos = s.find ( ' c ' ) ; 
    if ( pos != string : : npos )   
    {
        cout  <<  'c'  <<  " is in s" << pos << endl;
    }

    Note:
    Find method to find the character string s in C, if the return position of the found character string, if not found is returned NPoS
    NPoS string is a static member variable of the class definition to ensure that the value is greater than any significant the target value, can be viewed as an integer -1

  • rfind start looking forward position pos from the string of characters c, returns the position of the character in the string .... (others with find)
  • substr in str from the start position pos, taken n characters, then returned
    eg:
    substr(pos,n);
    substr(pos); //截取从pos到字符串结尾

Also only one of the more important ones (operator + =, c_str, find + npos) to focus on memory, several others can understand.

5. For non-member of the string class
herein merely illustrative effect:

  • operator + as little as possible, because the return value is passed, resulting in low efficiency of deep copy
  • operator >> (key) input operator overloading
  • operator << (focus) output operator overloading
  • getline (focus) get his string
  • relational operators (focus) compare the size

Indicate the need to master the use of the key part, will be used in the online OJ

String summary above is about the first part, ending attach a picture:
C ++ String summarizes the main components of the STL (the first portion, the construction and operation)

Guess you like

Origin blog.51cto.com/14232799/2447326