string转换为int
时间: 2023-12-31 13:03:43 浏览: 33
在C++中,可以使用`stoi()`函数将字符串转换为整数。例如:
```cpp
#include <iostream>
#include <string>
int main() {
std::string str = "123";
int num = std::stoi(str);
std::cout << num << std::endl;
return 0;
}
```
输出:
```
123
```
相关问题
string转化为Int
在Java中,可以使用Integer类的parseInt()方法将String转换为int类型。下面是一个示例代码:
```java
String str = "123";
int num = Integer.parseInt(str);
System.out.println(num); // 输出:123
```
在这个示例中,我们将字符串"123"转换为整数类型,并将其存储在变量num中。然后,我们使用System.out.println()方法将num的值打印出来。
string 转换为 int
可以使用 stoi 函数将 string 转换为 int,例如:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "123";
int num = stoi(str);
cout << "String转Int后的结果为:" << num << endl;
return 0;
}
```
输出结果为:
```
String转Int后的结果为:123
```
相关推荐
![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_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)