c++ string删除元素函数
时间: 2024-05-26 16:07:49 浏览: 25
C++ 中的 std::string 类型可以使用 erase() 函数来删除元素。该函数有两个重载版本:
1. erase(pos, n):从指定位置 pos 开始删除 n 个字符。
2. erase(iterator it):删除指定迭代器位置上的字符。
以下是使用 erase() 函数删除 std::string 中元素的示例代码:
```c++
#include <iostream>
#include <string>
int main() {
std::string str = "hello world";
str.erase(5, 1); // 从第5个位置开始删除1个字符,即删除空格
std::cout << str << std::endl; // 输出:helloworld
auto it = str.begin() + 5; // 获取迭代器指向第6个字符
str.erase(it); // 删除第6个字符
std::cout << str << std::endl; // 输出:helloorld
return 0;
}
```
相关问题
c++ string 的构造函数
C++中的string类是一个非常常用的字符串处理类,它提供了多种构造函数来创建和初始化字符串对象。下面是几种常见的构造函数:
1. 默认构造函数:string(),创建一个空字符串对象。
2. 字符串字面值构造函数:string(const char* str),使用C风格的字符串字面值来初始化字符串对象。
3. 复制构造函数:string(const string& str),使用另一个字符串对象来初始化当前对象。
4. 子串构造函数:string(const string& str, size_t pos, size_t len),使用另一个字符串对象的子串来初始化当前对象,从位置pos开始,长度为len。
5. 重复字符构造函数:string(size_t n, char c),创建一个包含n个重复字符c的字符串对象。
6. 范围构造函数:string(InputIt first, InputIt last),使用迭代器指定的范围内的字符来初始化字符串对象。
7. 移动构造函数:string(string&& str),使用右值引用来初始化当前对象,通常用于性能优化。
这些构造函数提供了不同的方式来创建和初始化string对象,可以根据具体的需求选择合适的构造函数来使用。
C++ string的所有函数举例
C++中的string类提供了许多函数来处理字符串。以下是一些常用的string函数及其示例:
1. length():返回字符串的长度。
示例:string str = "Hello";
int len = str.length(); // len的值为5
2. size():返回字符串的大小,与length()函数功能相同。
示例:string str = "Hello";
int size = str.size(); // size的值为5
3. empty():检查字符串是否为空。
示例:string str = "Hello";
bool isEmpty = str.empty(); // isEmpty的值为false
4. clear():清空字符串内容。
示例:string str = "Hello";
str.clear(); // 清空字符串,str变为空字符串
5. append():在字符串末尾添加字符或字符串。
示例:string str = "Hello";
str.append(" World"); // 字符串变为"Hello World"
6. insert():在指定位置插入字符或字符串。
示例:string str = "Hello";
str.insert(5, " World"); // 字符串变为"Hello World"
7. erase():删除指定位置的字符或一段字符。
示例:string str = "Hello World";
str.erase(5, 5); // 字符串变为"Hello"
8. replace():替换指定位置的字符或一段字符。
示例:string str = "Hello World";
str.replace(6, 5, "C++"); // 字符串变为"Hello C++"
9. substr():返回指定位置开始的子字符串。
示例:string str = "Hello World";
string subStr = str.substr(6); // subStr的值为"World"
10. find():查找指定字符或字符串的位置。
示例:string str = "Hello World";
int pos = str.find("World"); // pos的值为6
11. compare():比较两个字符串的大小。
示例:string str1 = "Hello";
string str2 = "World";
int result = str1.compare(str2); // result的值为负数
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)