[C++ Elementary]: string class

Basic usage of a string

in the document

insert image description here

insert image description here
insert image description here

general use

insert image description here

2. Iterator

insert image description here

1. Basic use

A very powerful operator [ ] is overloaded in string

insert image description here

This operator is essentially dereference, the operator that arrays use to access data. So we can operate on each character with this operator. Add a little knowledge: string is a class, and there are many member functions in the class. These member functions will be discussed below. Here, a size is first used to represent the length of the string.

access each character

insert image description here

insert image description here

iterator

insert image description here

insert image description here

2. Syntactic sugar

insert image description here

insert image description here

The principle here is to copy each character of s2 into ch, and auto will automatically deduce the type of ch. Because ch is a copied value, changing ch below will not change s2. If we need to change, we have to use &

insert image description here

insert image description here

This looks like a range for, but in fact the bottom layer is still an iterator.

3. Reverse iterator

insert image description here

insert image description here

4. const iterator

insert image description here

This is actually a problem of privilege amplification, const objects should also call const iterators.

insert image description here**insert image description here

To add: Why can const be modified, but it can be ++? Knowledge because const actually modifies *it, not it.

3. Capacity

insert image description here

insert image description here

We can see that it has a capacity function, which is the capacity. If the capacity is not enough, it will automatically expand the capacity. Different compilers have different expansion sizes. In vs2019, the capacity is expanded by 1.5 times each time. Obviously, the cost of such expansion is very high, and it is easy to run out of space, so it provides a reserve function.

insert image description here
insert image description here

There is also a resize function above, the difference between resize and reserve is: resize is open space + initialization (default is \0), reserve is simply open space.

insert image description here

insert image description here

4. Insertion and deletion

insert
insert image description here

insert image description here

delete

insert image description here

insert image description here

insert image description here

Use insert and erase with caution. Insertion and deletion are very inefficient and should be avoided if possible.

5. A sample question: Parsing protocol

find function
insert image description here

substr function
insert image description here

insert image description here

insert image description here

6. Read spaces

insert image description here

The string will automatically stop when it encounters a space when using cin to read. Using this function, you can read the space and control it freely.

insert image description here

7. Some other functions

insert image description here
string to integer
insert image description here

String to double
insert image description here
Others to string

insert image description here

Guess you like

Origin blog.csdn.net/m0_73790767/article/details/130819267