C++string转long long代码
时间: 2023-11-14 11:07:16 浏览: 215
可以使用 `std::stoll` 函数将字符串转换为 `long long` 类型。以下是一个简单的示例代码:
```c++
#include <iostream>
#include <string>
int main() {
std::string str = "1234567890";
long long num = std::stoll(str);
std::cout << num << std::endl;
return 0;
}
```
输出为:
```
1234567890
```
相关问题
c++ string 转longlong
可以使用 `std::stoll` 函数将字符串转换为 `long long` 类型。示例代码如下:
```c++
#include <iostream>
#include <string>
int main() {
std::string str = "1234567890";
long long num = std::stoll(str);
std::cout << num << std::endl; // 输出 1234567890
return 0;
}
```
注意,如果字符串中包含非数字字符,或者超出了 `long long` 类型的范围,转换会抛出 `std::invalid_argument` 或 `std::out_of_range` 异常。因此,在使用 `std::stoll` 函数时应当做好异常处理。
c++ string 转long
可以使用 `std::stol` 函数将 `std::string` 类型的字符串转换为 `long` 类型整数,示例代码如下:
```c++
#include <iostream>
#include <string>
int main() {
std::string str = "1234567890";
long num = std::stol(str);
std::cout << "num = " << num << std::endl;
return 0;
}
```
输出:
```
num = 1234567890
```
阅读全文
相关推荐
















