convert string to integer in c++(在 C++ 中将字符串转换为整数)
本文介绍了在 C++ 中将字符串转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好我知道有人问了很多次,但我没有找到具体问题的答案.
Hello I know it was asked many times but I hadn't found answer to my specific question.
我只想转换只包含十进制数字的字符串:
I want to convert only string that contains only decimal numbers:
例如 256 可以,但 256a 不行.
For example 256 is OK but 256a is not.
不检查字符串可以吗?
谢谢
推荐答案
我能想到的使错误检查成为可选的最简单方法是:
The simplest way that makes error checking optional that I can think of is this:
char *endptr;
int x = strtol(str, &endptr, 0);
int error = (*endptr != ' ');
这篇关于在 C++ 中将字符串转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:在 C++ 中将字符串转换为整数
基础教程推荐
猜你喜欢
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 从 std::cin 读取密码 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01