c++检查xs:double数据类型
c++ check xs:double datatype
我有以下形式的数据:
x="12.847.E.89"
y="12-1.2344e56"
现在我想知道x和y是否确认为xs:double数据类型http://www.w3.org/TR/xmlschema-2/#double.它们可能只有一个小数点和E,字符串开头有一个+或-,也可能有任何数量的字母数字字符。例如,这里,y是xs:double数据类型,而x不是xs:double。
我知道我可以使用:x.find('.')等来检查字符串中是否存在每个字符。但这只在字符存在或不存在的情况下给出。它没有给我指定或检查是否没有其他字符以.、+、-、,E存在,并且E、+、-本身出现一次,并且符合xs:双数据类型。是否可以在C++中使用任何标准库函数来执行同样的操作。
我使用的gcc版本是:gcc(Ubuntu/Linaro 4.6.4-6ubuntu2)4.6.4
stod()
采用第二个参数,该参数给出了它能够转换的字符数。您可以使用它来查看整个字符串是否已转换。这里有一个例子:
#include <iostream>
#include <string>
int main()
{
std::string good = "-1.2344e56";
std::string bad = "12.847.E.89";
std::string::size_type endPosition;
double goodDouble = std::stod(good, &endPosition);
if (endPosition == good.size())
std::cout << "string converted is: " << goodDouble << std::endl;
else
std::cout << "string cannot be converted";
double badDouble = std::stod(bad, &endPosition);
if (endPosition == good.size())
std::cout << "string converted is: " << badDouble << std::endl;
else
std::cout << "string cannot be converted";
std::cin.get();
return 0;
}
如果无法执行转换,则会引发invalid_argument异常。如果读取的值超出可表示值的范围一倍(在某些库实现中,这包括下溢),则抛出out_of_range异常。
相关文章:
- 防止主数据类型C++的隐式转换
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在C++中打印指向不同基元数据类型的指针的内存地址
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 如何计算数据类型的范围,例如int
- C++中数据类型修饰符的顺序
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- 特定数据类型的模板类
- 具有多个模板的模板函数,用于特定数据类型(如字符串)?
- 如果我获得Integer和Double的产品,输出的数据类型是什么
- OpenCL 的 clEnqueueReadBufferRect 适用于 int 但不适用于 double 数据类型
- 如何在C++中为integer、float和double数据类型同时重载运算符
- c++检查xs:double数据类型
- 混淆了double和float数据类型
- 比float或double更准确的数据类型?C++
- 从文件中读取数据类型string, int double.将每一行数据放入对象向量中
- 函数定义中使用的两种数据类型to_string(long double _Val)
- 如何从steady_clock返回经过的时间作为原始数据类型(double)
- 数据类型为float、float4、float8、double、double4、int、int4、int8的模板
- 何时使用 std::complex<long double> vs. 自己的复杂数据类型(结构等)