c++实现string
时间: 2024-06-01 22:05:53 浏览: 17
C++中的string是一个类,它封装了一个字符串,提供了各种方法来操作字符串。string类可以使用标准库中的头文件<string>来使用。以下是string类的一些常见操作:
1. 创建一个string对象
```
string str = "hello world"; // 使用一个字符串初始化
string str2; // 创建一个空的字符串
```
2. 获取字符串长度
```
int len = str.size(); // 获取字符串长度
```
3. 访问字符串中的字符
```
char c = str; // 访问第一个字符
```
4. 拼接两个字符串
```
string str3 = str + " " + "C++"; // 拼接两个字符串
```
5. 查找子字符串
```
int pos = str.find("world"); // 查找子字符串的位置
if (pos != string::npos) {
cout << "找到了" << endl;
}
```
6. 替换子字符串
```
str.replace(pos, 5, "CSDN"); // 替换子字符串
```
7. 字符串转换为数字
```
int num = stoi("123"); // 字符串转换为数字
```
相关问题
c++实现string类
C++中的string类是一个用于处理字符串的标准库类。它提供了一系列成员函数和操作符重载,使得字符串的操作更加方便和高效。
C++中的string类位于命名空间std中,因此在使用之前需要包含头文件< string >。
下面是一个简单的示例,展示了如何使用string类来创建、初始化和操作字符串:
```cpp
#include <iostream>
#include <string>
int main() {
// 创建一个空字符串
std::string str;
// 初始化字符串
std::string greeting = "Hello, world!";
// 获取字符串长度
int length = greeting.length();
std::cout << "Length: " << length << std::endl;
// 连接字符串
std::string name = "Alice";
std::string message = greeting + " My name is " + name;
std::cout << "Message: " << message << std::endl;
// 获取子串
std::string substring = message.substr(7, 5);
std::cout << "Substring: " << substring << std::endl;
// 查找子串
size_t position = message.find("world");
if (position != std::string::npos) {
std::cout << "Found at position: " << position << std::endl;
} else {
std::cout << "Not found" << std::endl;
}
return 0;
}
```
上述示例中,我们首先创建了一个空字符串`str`,然后使用赋值运算符将字符串"Hello, world!"赋给了变量`greeting`。接着,我们使用`length()`函数获取了字符串的长度,并使用`+`运算符将多个字符串连接起来形成新的字符串。我们还使用`substr()`函数获取了字符串的子串,并使用`find()`函数查找了子串在原字符串中的位置。
除了上述示例中的操作,string类还提供了许多其他有用的成员函数和操作符重载,如插入、删除、替换、比较等。你可以参考C++的官方文档或其他相关资料来了解更多关于string类的详细信息。
c++ 实现string转int
C++中可以使用`std::stoi()`函数将字符串转换为整数。
下面是示例代码:
```cpp
#include <iostream>
#include <string>
int main() {
std::string str = "123";
int num = std::stoi(str);
std::cout << num << std::endl; // 输出 123
return 0;
}
```
需要注意的是,如果转换的字符串不是合法的整数字符串,`std::stoi()`函数会抛出`std::invalid_argument`或`std::out_of_range`异常。因此,在转换前需要确保字符串是合法的整数字符串。
相关推荐
![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_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.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)