push_back sv
时间: 2023-09-09 10:10:01 浏览: 92
`push_back` and `sv` are both features in C++.
`push_back` is a method available on many standard C++ containers such as `std::vector`, `std::deque`, and `std::list`. It is used to add an element to the end of the container.
For example, if we have a vector of integers:
```
std::vector<int> vec {1, 2, 3};
```
We can add an additional element to the end using `push_back`:
```
vec.push_back(4);
```
Now the vector contains `{1, 2, 3, 4}`.
`sv` on the other hand, is a C++20 feature that stands for "string view". It is used to create a read-only view of a string without copying its contents.
For example, if we have a string:
```
std::string str = "hello world";
```
We can create a string view of the first 5 characters using `sv`:
```
std::string_view view = str.substr(0, 5);
```
Now `view` contains `"hello"`, but does not copy the contents of `str`. This can be useful for passing strings around without incurring the cost of copying them.
阅读全文