可视化 C++ 中 "atoi" 函数的简单示例
visual Simple example of the "atoi" function in c++
有人可以举一个非常简单的例子来说明如何使用 atoi 函数吗?我知道它应该如何工作,但大多数例子都在客观 C 中......我很难阅读,因为我还没有真正学会它. 谢谢,提前!
#include <cstdlib> // wraps stdlib.h in std, fixes any non-Standard content
std::string t1("234");
int i1 = std::atoi(t1.c_str());
int i1b = std::stoi(t1); // alternative, but throws on failure
const char t2[] = "123";
int i2 = std::atoi(t2);
const char* t3 = "-93.2"; // parsing stops after +/- and digits
int i3 = std::atoi(t3); // i3 == -93
const char* t4 = "-9E2"; // "E" notation only supported in floats
int i4 = std::atoi(t4); // i4 == -9
const char* t5 = "-9 2"; // parsing stops after +/- and digits
int i5 = std::atoi(t5); // i5 == -9
const char* t6 = "ABC"; // can't convert any part of text
int i6 = std::atoi(t6); // i6 == 0 (whenever conversion fails completely)
const char* t7 = "9823745982374987239457823987"; // too big for int
int i7 = std::atoi(t7); // i7 is undefined
由于最后一种情况下的行为是未定义的(可能是某些实现可以循环将下一个数字添加到前一个值的十倍,而无需花时间检查有符号整数溢出),因此建议改用std::stoi
或std::strtol
。
另请参阅:cpp首选项 atoi 用于包括示例在内的文档;stoi
相关文章:
- 在c++中用vector填充一个简单的动态数组
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的简单if-else语句是如何无法访问的代码
- 使用简单类型列表实现的指数编译时间.为什么
- 如何在BST的这个简单递归实现中消除警告
- 一种在C++中读取TXT配置文件的简单方法
- 关于简单C++函数(is_palindrome)的逻辑的问题
- 显示错误输出的简单数组排序程序
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 退出简单while循环时出现问题
- 为什么简单的算术减法在"if"条件下不起作用?
- C++-字符串是否包含一个带有简单循环的单词
- 关于 c++ 函数中指针赋值的简单问题
- 从函数返回任意简单类型的数据
- 如何在没有函数的情况下编写此代码并使C++更简单?
- 有没有办法简单地从 GPU 调用多个 cpp 输出文件?
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 当简单捕获中的标识符显示为参数的声明符 ID 时,没有编译器诊断
- 如何使用 samtools C API 构建一个简单的主.cpp文件
- 可视化 C++ 中 "atoi" 函数的简单示例