C + + Beginner Level -- Introduction to STL + (string)

1) Getting started with STL ① STL concept Concept: STL (Standard Template Library): it is an important part of the C + +...

1) Getting started with STL

① STL concept

Concept: STL (Standard Template Library): it is an important part of the C + + standard library. It is not only a reusable component library, but also a software framework including data structures and algorithms

② STL version:

  1. Original version
    Alexander Stepanov and Meng Lee completed the original version in HP Labs. In the spirit of open source, they declare that anyone is allowed to arbitrarily use, copy, modify, disseminate and commercially use these codes without paying. The only condition is that it needs to be used as open source as the original version. HP version – the ancestor of all STL implementation versions.
  2. P. J. version
    Developed by P. J. Plauger and inherited from HP version, it is adopted by Windows Visual C + + and cannot be disclosed or modified. Defects: low readability and strange symbol naming.
  3. RW version
    Developed by Rouge Wage and inherited from HP version, it is adopted by C+ + Builder and cannot be disclosed or modified. It is generally readable.
  4. SGI version
    Developed by Silicon Graphics Computer Systems, Inc. and inherited from HP version. It is adopted by GCC(Linux) with good portability and can be published, modified or even sold. From the perspective of naming style and programming style, it is very readable

③ STL six components

  1. Imitation function:
    Including: grater less
  2. Algorithm:
    Including: find swap reverse
  3. Iterator:
    Including: iterator const_iterator reverse_iterator…
  4. Space configurator:
    Including: allocator
  5. Container (data structure):
    Including: string vector list
  6. Adapter:
    Including: stack queue priority_queue
2)string

1. Introduction to string:

  1. A string is a class that represents a sequence of characters
  2. The standard string class provides support for such objects. Its interface is similar to that of the standard character container, but adds a design feature specifically for manipulating single byte character strings
  3. The string class uses char (that is, as its character type, its default char_traits and allocator type (for more information about templates, see basic_string)
  4. String class is an instance of basic_string template class. It uses char to instantiate basic_string template class, and uses char_traits and allocator as default parameters of basic_string (for more template information, please refer to basic_string)
    From cplusplus.com
  5. Note that this class processes bytes independently of the encoding used: if it is used to process sequences of multi byte or variable length characters (such as UTF-8), all members of this class (such as length or size) and its iterators will still operate according to bytes (rather than actually encoded characters)

2. Precautions

1 cannot operate on sequences of multibyte or variable length characters
2. When using string class, it must include #include header file and using namespace std

3. string usage

① string constructor

Default (1) string()
Copy (2) string (const string & STR);
String (3) string (const string & STR, size_t POS, size_t len = NPOs);
From const string (4) string (const char* s);
From sequence (5) string (const char* s, size_t n);
Fill in (6) string (size_t n, char c);
Range (7) template < class inputiterator >
string (InputIterator first, InputIterator last);

(1) Empty string constructor (default constructor): construct an empty string with a length of zero characters
(2) Copy construction: the function constructs a copy of str
(3) String: the constructor copies the part of str that starts at the character position pos and spans len characters (or until the end of str, if str is too short or len is string::npos)
(4) Copy the null terminated character sequence (C-string) pointed to by s from const string:
(5) From buffer: copy the first n characters from the character array pointed to by s
(6) Fill constructor: fills a string with n consecutive copies of the character c
(7) Range constructor: copies the sequence of characters in the range (first,last) in the same order
(8) Initialization list: copy each character in il in the same order
(9) Move constructor: gets the contents of str. STR is in an unspecified but valid state

All the above constructors support the object of member type allocator_type as an additional optional parameter at the end, which is irrelevant to string (not shown above, see the constructor of basic_string for complete signature)

Example:

int main () { std::string s0 ("Initial string"); // constructors used in the same order as described above: std::string s1; std::string s2 (s0); std::string s3 (s0, 8, 3); std::string s4 ("A character sequence"); std::string s5 ("Another character sequence", 12); std::string s6a (10, 'x'); std::string s6b (10, 43); // 43 is the ASCII code for '+' std::string s7 (s0.begin(), s0.begin()+7); std::cout << "s1: " << s1 << "\ns2: " << s2 << "\ns3: " << s3; std::cout << "\ns4: " << s4 << "\ns5: " << s5 << "\ns6a: " << s6a; std::cout << "\ns6b: " << s6b << "\ns7: " << s7 << '\n'; return 0; }

Output:
s1:
s2: Initial string
s3: str
s4: A character sequence
s5: Another char
s6a: xxxxxxxxxx
s6b: ++++++++++
s7: Initial

② Common capacity operations for string class objects size length capacity namefunctionsizeReturns the valid character length of a stringlengthReturns the valid character length of a stringcapacityReturns the total size of the space
{ string s("string"); cout<<s.size()<<endl; cout << s.length() << endl; cout << s.capacity() << endl;


be careful:

  1. Valid string length does not include '\ 0'
  2. Length is reserved because of the habit of C language. At first, the string class only had length. After the introduction of STL, size was added for compatibility to comply with the interface rules of STL
    When used, size() is generally used as a method to return the size of the container, and length() is generally used to return the length of a sequence, but the results returned by both are the same
clear namefunctionclearClear valid characters
string s("string"); cout<<s.size()<<endl; cout << s.capacity() << endl; s.clear(); cout << s.size() << endl; cout << s.capacity() << endl;


be careful:

  1. When emptying, only clear the size to 0 without changing the size of the underlying space
resize namefunctionresizeConvert the number of valid characters into n, and the extra space is filled with character c
string s("string"); cout<<s.size()<<endl; cout << s.capacity() << endl; s.resize(20); cout << s.size() << endl; cout << s.capacity() << endl; s.resize(4); cout << s << endl; cout << s.size() << endl; cout << s.capacity() << endl; s.resize(20,'a');//The excess is filled with 'a' cout << s << endl; cout << s.size() << endl; cout << s.capacity() << endl;


be careful:

  1. resize extra space will be filled with '\ 0'
  2. If the space is not enough, it will be automatically expanded (so when we want to open up space and initialize the space, we can use resize), and the expansion under VS compiler is 1.5 times each time (g + + is 2 times at a time), which will be discussed later
reserve namefunctionreserveReserve space for string
string s; cout<<s.size()<<endl; cout << s.capacity() << endl; s.reserve(200); cout << s.size() << endl; cout << s.capacity() << endl; s.reserve(100); cout << s.size() << endl; cout << s.capacity() << endl;

string s; size_t sz = s.capacity(); for (int i = 0; i < 100; ++i) { s.push_back('c'); if (sz != s.capacity()) { sz = s.capacity(); cout << "capacity: " << sz << '\n'; } }


Operation:
If we know the size of the space to be opened, we should open it at one time to avoid capacity increase and improve efficiency
be careful:

  1. reserve(size_t res_arg=0): reserve space for string without changing the number of effective elements. When the reserve parameter is less than the total size of the underlying space of string, the reserve will not change the capacity
  2. Do not use this function easily. The expansion and reduction of string are CPU intensive operations, which have an impact on efficiency
  3. NSize in string.reserve(int nSize) needs to ensure that it can accommodate enough characters in string, otherwise this method is invalid (note that it will not crash)
  4. In VS, the capacity will be expanded by 1.5 times, and the occupied space of string will be automatically aligned to the integer step size after setting string.reserve(int nSize)
    For example, 20 will be aligned to 2 * 16-1 = 31 (1????)
  5. 2X capacity increase in g + +
other namefunctionemptyIf the detection string is released as an empty string, return true; otherwise, return falsemax_sizeReturns the maximum size of a stringshrink_to_fitShrink to fit

Specific reference: cplusplus.com string

③ Access and traversal of string class objects namefunctionoperator[]Returns the character of the pos position, which is called by the const string class objectbegin+endbegin gets the iterator of one character, and end gets the iterator of the next position of the last characterrbegin+rendbegin gets the iterator of one character, and end gets the iterator of the next position of the last characterRange forC++11 supports a new traversal method of more concise range for

The following are three traversal methods: (note that in addition to traversing the string object, the following three methods can also traverse and modify the characters in the string)
Note: the first of the following three methods is the most used for string s

string s("hello Bit"); // 1. for+operator[] for (size_t i = 0; i < s.size(); ++i) cout << s[i] << endl; // 2. Iterator 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; // 3. Scope for for (auto ch : s) cout << ch << endl;
iterator

Const objects should use const iterators. They are read-only and cannot be written

string s("hello"); string::const_iterator it = s.cbegin(); while (it != s.cend()) { //*it += 1; cout << *it << " "; ++it; } cout << endl;

rbegin and rend

string::const_iterator it = s.begin(); while (it != s.end()) { //*it += 1; cout << *it << " "; ++it; } cout << endl; //string::const_reverse_iterator rit = s.rbegin(); auto rit = s.rbegin(); while (rit != s.rend()) { //*rit += 1; cout << *rit << " "; ++rit; } cout << endl;

Note that here cbegin and cend can also be directly used as begin and end(const c can be omitted directly)
(the following interfaces are added in C++11 to match)

④ Modification of string class object push_back append += namefunctionpush_backInsert character c after stringappendAppend a string after the stringoperator+=Append string str after stringinsertUse lesseraseUse less


Note that insert and erase are rarely used, because the data needs to be moved when the data is in the head and in the middle, which is inefficient

c_str namefunctionc_strReturns a C format string
string s; /*char* c; string s1 = "1234"; c = s.c_str();*///Error here //c_str() returns an address assigned to const char *, //Its content has been set as unchangeable. If this address is assigned to a char * variable that can change the content, a conflict will occur char c[20]; string s1 = "1234"; strcpy(c, s.c_str()); cout << s1.c_str() << endl;

be careful:
c_ The str () function returns a pointer to the normal C string, with the same content as this string

find rfind sub_str namefunctionfind and nposFind the character c from the pos position of the string and return the position of the character in the stringrfindFind the character c from the pos position of the string, and return the position of the character in the string (find the last content in the string)sub_strIn str, n characters are intercepted from the pos position, and then returned

NPOs is a static member variable static const size in string_ t npos = -1;

Example (take out the protocol, domain name and uri in the url):

string url("https://blog.csdn.net/lplp90908/article/details/49998181/"); size_t i1 = url.find("://"); if (i1 != string::npos) { string protocol = url.substr(0, i1 - 0); cout << "protocol:" << protocol << endl; } size_t i2 = url.find('/', i1 + 3); if (i2 != string::npos) { string domain = url.substr(i1 + 3, i2 - (i1 + 3)); cout << "domain:" << domain << endl; } string uri = url.substr(i2); cout << "uri:" << uri << endl;


be careful:

  1. When appending characters to the end of a string, s.push_back(c) / s.append (1, c) / S + ='c 'are implemented in the same way. Generally, the string class has more + = operations. The + = operation can connect not only a single character, but also a string
⑤ string class nonmember function getline namefunctiongetlineGet one line of string

getline scenario: when cin encounters a space, it ends reading

string line; // while(cin>>line) while(getline(cin, line)) { size_t pos = line.rfind(' '); cout<<line.size()-pos-1<<endl; }
other namefunctionoperator+Use as little as possible, because the value is returned, resulting in low efficiency of deep copyoperator>>+operator<<Input operator overloadrelational operatorsSize comparison

26 October 2021, 11:01 | Views: 1449

Add new comment

For adding a comment, please log in
or create account

0 comments