C++ std::string 和格式字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1575698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:30:12  来源:igfitidea点击:

std::string and format string

c++string-formatting

提问by Scott

I found the below code through Google. It almost does what I want it to do, except it doesn't provide a way to indicate the precision like '%.*f' does in C-type format strings. Also, it doesn't provide anything further than 5 decimal places. Am I going to have to stick with C strings and snprintf?

我通过谷歌找到了下面的代码。它几乎可以完成我想要它做的事情,只是它没有提供一种方法来指示像 '%.*f' 在 C 类型格式字符串中所做的精度。此外,它不提供超过 5 个小数位的任何内容。我将不得不坚持使用 C 字符串和 snprintf 吗?

#include <string>
#include <sstream>
#include <iostream>

template <class T>
std::string to_string(T t, std::ios_base & (*f)(std::ios_base&))
{
  std::ostringstream oss;
  oss << f << t;
  return oss.str();
}

int main()
{
  std::cout<<to_string<double>(3.1415926535897931, std::dec)<<std::endl;
  return 0;
} 

采纳答案by 1800 INFORMATION

You want to use the std::setprecisionmanipulator:

你想使用std::setprecision操纵器:

int main()
{
    std::cout << std::setprecision(9) << to_string<long>(3.1415926535897931, std::dec)
              << '\n';
    return 0;
}

回答by GManNickG

C++ wouldn't be successful if it couldn't do something C could.

如果 C++ 不能做 C 可以做的事情,它就不会成功。

You need to check out manipulators.

你需要检查操纵器

If you want C-style formatting (which I do prefer, it's more terse), check out Boost.Format.

如果你想要 C 风格的格式(我更喜欢,它更简洁),请查看Boost.Format

回答by MSalters

Taking the almost-correct answer (note that std::dec is redundant in this simple case):

采取几乎正确的答案(注意 std::dec 在这个简单的情况下是多余的):

int main()
{
  std::cout << std::setprecision(9) << std::dec << 3.1415926535897931 << std::endl;
  return 0;
}

However, if you wanted the to_string function to behave as desired, that's a bit more difficult. You'd need to pass setprecision(9)to the to_string<T>function, and it doesn't accept arguments of that type. You'd want a templated version:

但是,如果您希望 to_string 函数按预期运行,那就有点困难了。您需要传递setprecision(9)给该to_string<T>函数,并且它不接受该类型的参数。你想要一个模板版本:

template <class T, class F>
std::string to_string(T t, F f)
{
  std::ostringstream oss;
  oss << f << t;
  return oss.str();
}
int main()
{
  std::cout << to_string<double>(3.1415926535897931, std::setprecision(9)) << std::endl;
  return 0;
}

This works because you really didn't need std::dec in to_string. But if you needed to pass more manipulators, the simple solution is to add template <class T, class F1, class F2> std::string to_string(T t, F1 f1, F2 f2)etcetera. Technically this doesn't scale very well, but it's going to be so rare that you probably don't need it at all.

这是有效的,因为您确实不需要在 to_string 中使用 std::dec。但是如果你需要传递更多的操纵器,简单的解决方案是添加template <class T, class F1, class F2> std::string to_string(T t, F1 f1, F2 f2)等等。从技术上讲,这不能很好地扩展,但它会非常罕见,以至于您可能根本不需要它。

回答by Jerry Coffin

Have you looked at Boost::format?

你看过Boost::format吗?

Edit: It's not entirely clear what you want. If you just want to write to a string, with formatting, you can use normal manipulators on a stringstream. If you want to use printf-style formatting strings, but retain type-safety, Boost::format can/will do that.

编辑:目前还不完全清楚你想要什么。如果您只想写入带有格式的字符串,则可以在字符串流上使用普通操纵器。如果你想使用 printf 风格的格式化字符串,但保留类型安全性,Boost::format 可以/将会这样做。