How do you convert a C++ string to an int?(如何将 C++ 字符串转换为 int?)
本文介绍了如何将 C++ 字符串转换为 int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能的重复:
如何在 C++ 中将字符串解析为 int?
如何将 C++ 字符串转换为 int?
How do you convert a C++ string to an int?
假设您希望字符串中包含实际数字(例如1"、345"、38944").
Assume you are expecting the string to have actual numbers in it ("1", "345", "38944", for example).
另外,假设您没有 boost,并且您真的想用 C++ 方式来实现,而不是笨拙的旧 C 方式.
Also, let's assume you don't have boost, and you really want to do it the C++ way, not the crufty old C way.
推荐答案
#include <sstream>
// st is input string
int result;
stringstream(st) >> result;
这篇关于如何将 C++ 字符串转换为 int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何将 C++ 字符串转换为 int?


基础教程推荐
猜你喜欢
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 我有静态或动态 boost 库吗? 2021-01-01