std::string构造函数有哪些
时间: 2023-08-10 12:03:29 浏览: 53
`std::string` 类有多个构造函数,可以根据不同的参数类型和数量来创建字符串对象。以下是一些常见的 `std::string` 构造函数:
1. 默认构造函数:
```cpp
std::string();
```
创建一个空的字符串对象。
2. 字符串字面值构造函数:
```cpp
std::string(const char* str);
std::string(const char* str, size_t len);
```
从 C 风格的字符串(以空字符 '\0' 结尾)或字符数组创建字符串对象。
3. 复制构造函数:
```cpp
std::string(const std::string& str);
```
从现有的 `std::string` 对象创建一个新的字符串对象。
4. 移动构造函数:
```cpp
std::string(std::string&& str);
```
从一个临时的 `std::string` 对象创建一个新的字符串对象。
5. 字符重复构造函数:
```cpp
std::string(size_t n, char c);
```
创建一个由字符 `c` 重复 `n` 次组成的字符串对象。
6. 范围构造函数:
```cpp
template<class InputIterator>
std::string(InputIterator first, InputIterator last);
```
从迭代器范围内的字符序列创建字符串对象。
除了上述列举的构造函数之外,`std::string` 类还提供了其他重载的构造函数,用于更灵活地创建字符串对象。
希望这个解答对你有帮助!如果你还有其他问题,请随时提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)