- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
_wtoi当不能转换输入,所以输入不是整数时,返回零。但同时输入可以为零。这是一种确定输入是否错误或为零的方法吗?
最佳答案
这是 C++,您应该使用 stringstream
进行转换:
#include <iostream>
#include <sstream>
int main()
{
using namespace std;
string s = "1234";
stringstream ss;
ss << s;
int i;
ss >> i;
if (ss.fail( ))
{
throw someWeirdException;
}
cout << i << endl;
return 0;
}
boost 的 lexical_cast
有一个更简洁、更简单的解决方案:
#include <boost/lexcal_cast.hpp>
// ...
std::string s = "1234";
int i = boost::lexical_cast<int>(s);
如果你坚持使用 C,sscanf
可以干干净净。
const char *s = "1234";
int i = -1;
if(sscanf(s, "%d", &i) == EOF)
{
//error
}
您也可以使用 strtol
,但需要一点思考。是的,它会为两个评估为零的字符串和错误返回零,但它还有一个(可选)参数 endptr
它将指向已转换的数字之后的下一个字符:
const char *s = "1234";
const char *endPtr;
int i = strtol(s, &endPtr, 10);
if (*endPtr != NULL) {
//error
}
关于c++ - _wtoi 返回零 : input zero or non-numerical input?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10688805/
翻译: 用法:zeros(shape, dtype=float, order='C') 返回:返回来一个给定形状和类型的用0填充的数组; 参数:shape:形状 dtype:数据类型,可选参
我想像这样格式化一个 double: 1.23 => 1.2 1.0 => 1 0.4 => 0.4 0 => 0 对应的字符串格式是什么?我目前正在使用 StringFormat={}{0
在 simple geometric program 中用 Javascript 和 Canvas 编写,当我将 Angular 设置为 270° (1½π) 时,我预计 Math.cos(θ) 会变
我们有一些基于 Linux (Centos) 的虚拟机,它们将用作可分发的虚拟设备。我们希望能够尽可能地压缩它们以便分发(通过 tar.gz、zip 等)。 我们删除了所有不必要的文件(.log's、
之前有一个问题,它得到了答案: 感谢那。 “现在我已经格式化了我的单元格: h "小时"m "分钟" 因此,如果我的单元格有 7:00,它会显示为 7 小时 0 分钟。如果小时或分钟为零,有没有办法删
这个问题已经有答案了: C program to convert Fahrenheit to Celsius always prints zero (6 个回答) 已关闭 4 年前。 我的以下简单编码
我有一个类的以下代码。这是一个类的初始化。 第三方动态链接库 [DllImport("gdi32.dll")] public static extern IntPtr CreateCompatib
这是我书中的一段代码,我不确定匹配是如何工作的,因为它似乎第一个案例匹配所有内容。以下是 Ocaml 向我提出的警告: # let zero = 0;; # let one = 1;; # let r
我正在尝试重构一些现有代码into a more monodic approach 。现有代码包含接口(interface) IXInterface 和数字,例如 int 和 bool。默认情况下,数
我一直在考虑单词序列的 0 填充以及如何将 0 填充转换为嵌入层。乍一看,人们会认为您也希望保持嵌入 = 0.0。但是,keras 中的嵌入层会为任何输入标记生成随机值,并且无法强制其生成 0.0。请
我正在尝试使用 Pandas 解决以下 python 面试问题: 给定一个 m x n 矩阵,如果一个元素为 0,则将其整个行和列设置为 0。就地执行。 这里有一些例子: # Example 1 [[
我正在优化我正在编写的程序中最耗时的循环,该循环对数组中的许多条目求和,其中许多条目将为零。在添加之前检查条目是否为零或跳过检查并添加所有条目是否更快?下面每一个的例子。这是在 C++ 中。谢谢! d
之前(作为菜鸟)我将它作为 R 包错误提交,让我由你们来运行它。我认为以下所有内容都很好: replace_number("123 0 boogie") [1] "one hundred twenty
默认情况下,在BPI零M2上禁用eth0。。在这里,我们将演示如何启用它
我有一个 PG 数据库表价格。结构如下: id name total_sales created_at 1 A 0.0 2016-01-01
这个问题在这里已经有了答案: Difference between numpy.array shape (R, 1) and (R,) (8 个答案) 关闭 6 年前。 有什么区别 numpy.ze
是否可以通过 Skype 用户窗口获取处理程序并使用 SendMessage(whdl,BM_CLICK,intptr.zero,intrptr.zero,intptr.zero) 单击发送文件或调用
我使用开箱即用的 MVC 4 简单成员资格。我对网站做了很多修改,现在我要回去清理,我发现我不能再修改我的密码了。我一定是视而不见,因为我认为这应该很容易解决,但我只花了 2 天时间解决这个问题。 我
我是CorePlot的新手,终于搞定了一些散线图显示。如何将 X 轴设置为零并位于图形底部,将 Y 轴设置为零并位于图形左侧? 最佳答案 将 plotSpace 的 xRange 设置为 plotSp
我已经为数据表实现了 LazyLoading。当我使用分页浏览数据表时,出现以下异常。 com.sun.faces.context.PartialViewContextImpl processPart
我是一名优秀的程序员,十分优秀!