C++ 将字符串转换为double和int。如何转换?
我无法将字符串转换为双精度字符串。 我的字符串已使用“string”函数声明,因此我的字符串为:C++ 将字符串转换为double和int。如何转换?,c++,C++,我无法将字符串转换为双精度字符串。 我的字符串已使用“string”函数声明,因此我的字符串为: string marks = ""; 现在,为了将其转换为double,我在互联网上的某个地方找到了使用word.c_str()的方法,我就这样做了。我这样称呼它并使用它: doubleMARK = strtod( marks.c_str() ); char sfloat[] = "1.0"; float x; sscanf (sfloat, "%lf", &x); // REFERE
string marks = "";
现在,为了将其转换为double,我在互联网上的某个地方找到了使用word.c_str()的方法,我就这样做了。我这样称呼它并使用它:
doubleMARK = strtod( marks.c_str() );
char sfloat[] = "1.0";
float x;
sscanf (sfloat, "%lf", &x);
// REFERENCE: http://www.codeguru.com/forum/showthread.php?t=231054
include <string>
#include <sstream>
#include <iostream>
template <class T>
bool from_string(T& t,
const std::string& s,
std::ios_base& (*f)(std::ios_base&))
{
std::istringstream iss(s);
return !(iss >> f >> t).fail();
}
int main()
{
int i;
float f;
// the third parameter of from_string() should be
// one of std::hex, std::dec or std::oct
if(from_string<int>(i, std::string("ff"), std::hex))
{
std::cout << i << std::endl;
}
else
{
std::cout << "from_string failed" << std::endl;
}
if(from_string<float>(f, std::string("123.456"), std::dec))
{
std::cout << f << std::endl;
}
else
{
std::cout << "from_string failed" << std::endl;
}
return 0;
}
这与我在web上找到的示例类似:
n1=strtod( t1.c_str() );
显然,事情就是这样发生的。但当然,它不起作用。我需要另一个参数。我相信是指针?但我现在不知道该怎么办。它需要一个地方来储存价值还是什么?还是什么
我还需要将这个字符串转换成一个整数,我还没有开始研究如何转换,但是一旦我发现并且有错误,我会将其编辑出来并发布到这里。你不使用and的原因是什么?它们至少比脆弱的strtod强大9级
如果必须使用
strtod
,则只需将NULL
作为第二个参数传递即可。根据cplusplus.com:
如果[第二个参数]不是空指针,该函数还将endptr所指的值设置为指向数字后的第一个字符
而且它不需要非空NULL在旧的黑暗的C时代,我会做一些丑陋和不安全的事情,比如:
doubleMARK = strtod( marks.c_str() );
char sfloat[] = "1.0";
float x;
sscanf (sfloat, "%lf", &x);
// REFERENCE: http://www.codeguru.com/forum/showthread.php?t=231054
include <string>
#include <sstream>
#include <iostream>
template <class T>
bool from_string(T& t,
const std::string& s,
std::ios_base& (*f)(std::ios_base&))
{
std::istringstream iss(s);
return !(iss >> f >> t).fail();
}
int main()
{
int i;
float f;
// the third parameter of from_string() should be
// one of std::hex, std::dec or std::oct
if(from_string<int>(i, std::string("ff"), std::hex))
{
std::cout << i << std::endl;
}
else
{
std::cout << "from_string failed" << std::endl;
}
if(from_string<float>(f, std::string("123.456"), std::dec))
{
std::cout << f << std::endl;
}
else
{
std::cout << "from_string failed" << std::endl;
}
return 0;
}
在C++中,你可以这样做:< /P>
doubleMARK = strtod( marks.c_str() );
char sfloat[] = "1.0";
float x;
sscanf (sfloat, "%lf", &x);
// REFERENCE: http://www.codeguru.com/forum/showthread.php?t=231054
include <string>
#include <sstream>
#include <iostream>
template <class T>
bool from_string(T& t,
const std::string& s,
std::ios_base& (*f)(std::ios_base&))
{
std::istringstream iss(s);
return !(iss >> f >> t).fail();
}
int main()
{
int i;
float f;
// the third parameter of from_string() should be
// one of std::hex, std::dec or std::oct
if(from_string<int>(i, std::string("ff"), std::hex))
{
std::cout << i << std::endl;
}
else
{
std::cout << "from_string failed" << std::endl;
}
if(from_string<float>(f, std::string("123.456"), std::dec))
{
std::cout << f << std::endl;
}
else
{
std::cout << "from_string failed" << std::endl;
}
return 0;
}
<> > C++流提供了各种各样的转换
类型。将字符串与流一起使用的方法是使用stringstream:
你看过strtod()的文档了吗??没有,但我会马上开始。嗯,我没有使用它们的原因是因为我从来没有学过它们,或者听说过它们,它们也从来没有出现在我的谷歌冒险中。想解释一下它们是如何工作的吗?还是我应该回到我穿越谷歌的伟大旅程?非常感谢。@Robsta是的,我只在这里了解了他们。您可以单击答案中的函数名称,转到每个函数的文档页面。如果你不能破译文件,请告诉我,我会给你一个答复example@J.F.Sebastian非常感谢,我从不介意人们编辑我的答案来改进它们。我碰巧喜欢strod():)据我所知,您所需要的只是要转换的字符串和指针?然后你得到了你想要的返回值?我怎么做到的?这个。这也有助于垃圾装载。伙计,谢谢你的时间和投入,我很感激(: