Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C+中将float转换为std::string+;_C++_Type Conversion - Fatal编程技术网

C++ 在C+中将float转换为std::string+;

C++ 在C+中将float转换为std::string+;,c++,type-conversion,C++,Type Conversion,我有一个浮点值需要放入std::string中。如何将浮点转换为字符串 float val = 2.5; std::string my_val = val; // error here 除非您担心性能,否则请使用: #包括 //.. std::ostringstream ss; ss如果您担心性能,请查看库。您可以定义一个模板,该模板不仅适用于double,还适用于其他类型 template <typename T> string tostr(const T& t) {

我有一个浮点值需要放入
std::string
中。如何将浮点转换为字符串

float val = 2.5;
std::string my_val = val; // error here

除非您担心性能,否则请使用:

#包括
//..
std::ostringstream ss;

ss如果您担心性能,请查看库。

您可以定义一个模板,该模板不仅适用于double,还适用于其他类型

template <typename T> string tostr(const T& t) { 
   ostringstream os; 
   os<<t; 
   return os.str(); 
} 

< C++ > 11,标准C++库提供了各种支持类型的函数,用于<代码> ARG。

给出了一个简单而优雅的解决方案,我转录如下:

#include <sstream>
#include <string>
#include <stdexcept>

class BadConversion : public std::runtime_error {
public:
  BadConversion(std::string const& s)
    : std::runtime_error(s)
    { }
};

inline std::string stringify(double x)
{
  std::ostringstream o;
  if (!(o << x))
    throw BadConversion("stringify(double)");
  return o.str();
}
...
std::string my_val = stringify(val);
#包括
#包括
#包括
类BadConversion:public std::runtime\u错误{
公众:
BAD转换(标准::字符串常量(&s)
:std::运行时错误
{ }
};
内联标准::字符串字符串化(双x)
{
std::ostringstream o;

如果(!(o重要
读最后的注释

快速回答:
使用
创建字符串()
(从c++11开始提供)
例如:

#include <iostream>   
#include <string>  

using namespace std;
int main ()
{
    string pi = "pi is " + to_string(3.1415926);
    cout<< "pi = "<< pi << endl;

  return 0;
}
重要提示:
正如@Michael Konečný正确地指出的,使用
到_string()
充其量是有风险的,这很可能导致意外的结果。
发件人:

使用浮点类型
std::to_string
可能会产生意外结果,因为返回字符串中的有效位数可能为零,请参见示例。
返回值可能与默认情况下打印的值有很大差异,请参见示例。
std::to_string
依赖于当前区域设置进行格式化,因此从 多线程可能导致调用的部分序列化。
C++17
提供
std::to_chars
作为独立于语言环境的更高性能 另类

最好的方法是使用
stringstream
,如中所示的@dcp

以下示例演示了此问题:
自己运行示例:

可以在C++11中使用

一旦标准库提供,请使用:

std::array<char, 32> buf;
auto result = std::to_chars(buf.data(), buf.data() + buf.size(), val);
if (result.ec == std::errc()) {
  auto str = std::string(buf.data(), result.ptr - buf.data());
  // use the string
} else {
  // handle the error
}

如果标准提案获得批准,C++20可能会获得一个更方便的
std::format
API,其好处与
std::to_chars
相同。

考虑到很少调用这些函数(调整窗口大小),这将是一个适当的解决方案,但是有没有更有效的方法呢?如果您对Boost不满意,请编写您自己的词法转换函数;它大约有五行代码,是最有用的库函数(请参阅基本实现)对于第一种方法,不要忘记<代码>包含< /C> > .Java11111,头是一个非常重要的点,所有这些都是从答案中经常省略的。这对于C++的流来说尤其有问题。谢谢你指出。如果你没有的话,我会这么做。考虑读Habor萨特的文章“庄园农场的字符串格式”()。。它提供了五种最常用的格式化方法的示例,并讨论了它们的优缺点。我认为您的意思是“如果您不担心性能”。
boost::lexical\u cast
是您可以选择的最重的解决方案!请注意std::to\u string()可能出现的意外行为当使用浮点数时。(请参阅此链接上的示例输出)。这应该被提升为最佳答案,因为大多数构建环境都支持C++11,因为在使用浮点数时,已经意识到std::to_string()可能出现意外行为。(请参阅此链接上的示例输出)请注意,不要复制粘贴提交宽限期编辑答案。复制粘贴编辑提交答案。我们会收到一个自动标志。下次小心。这与@dmckee 2013年的答案基本相同。@Yochai当我
cout
时,
my_val
给出5.20000,但当你将
val
指定为50.2或更大时,则给出5.20000给出的输出不准确。给出的是:50.000001和107.199997(对于107.2)。这是什么原因?
#include <iostream>   
#include <string>  

using namespace std;
int main ()
{
    string pi = "pi is " + to_string(3.1415926);
    cout<< "pi = "<< pi << endl;

  return 0;
}
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);
#include <iostream>
#include <sstream>
#include <string>

template < typename Type > std::string to_str (const Type & t)
{
  std::ostringstream os;
  os << t;
  return os.str ();
}

int main ()
{

  // more info : https://en.cppreference.com/w/cpp/string/basic_string/to_string
  double    f = 23.43;
  double    f2 = 1e-9;
  double    f3 = 1e40;
  double    f4 = 1e-40;
  double    f5 = 123456789;
  std::string f_str = std::to_string (f);
  std::string f_str2 = std::to_string (f2); // Note: returns "0.000000"
  std::string f_str3 = std::to_string (f3); // Note: Does not return "1e+40".
  std::string f_str4 = std::to_string (f4); // Note: returns "0.000000"
  std::string f_str5 = std::to_string (f5);

  std::cout << "std::cout: " << f << '\n'
    << "to_string: " << f_str << '\n'
    << "ostringstream: " << to_str (f) << "\n\n"
    << "std::cout: " << f2 << '\n'
    << "to_string: " << f_str2 << '\n'
    << "ostringstream: " << to_str (f2) << "\n\n"
    << "std::cout: " << f3 << '\n'
    << "to_string: " << f_str3 << '\n'
    << "ostringstream: " << to_str (f3) << "\n\n"
    << "std::cout: " << f4 << '\n'
    << "to_string: " << f_str4 << '\n'
    << "ostringstream: " << to_str (f4) << "\n\n"
    << "std::cout: " << f5 << '\n'
    << "to_string: " << f_str5 << '\n'
    << "ostringstream: " << to_str (f5) << '\n';

  return 0;
}
std::cout: 23.43
to_string: 23.430000
ostringstream: 23.43

std::cout: 1e-09
to_string: 0.000000
ostringstream: 1e-09

std::cout: 1e+40
to_string: 10000000000000000303786028427003666890752.000000
ostringstream: 1e+40

std::cout: 1e-40
to_string: 0.000000
ostringstream: 1e-40

std::cout: 1.23457e+08
to_string: 123456789.000000
ostringstream: 1.23457e+08 
float val = 2.5;
std::string my_val = std::to_string(val);
std::array<char, 32> buf;
auto result = std::to_chars(buf.data(), buf.data() + buf.size(), val);
if (result.ec == std::errc()) {
  auto str = std::string(buf.data(), result.ptr - buf.data());
  // use the string
} else {
  // handle the error
}
auto s1 = std::to_string(1e+40);
// s1 == 10000000000000000303786028427003666890752.000000

auto s2 = std::to_string(1e-40);
// s2 == 0.000000