C++ 字符串格式,如 Python "{}".format
C++ string formatting like Python "{}".format
我正在寻找一种快速、简洁的方式来打印漂亮的 table 格式,单元格正确对齐。
在c++中有没有一种方便的方法来创建具有一定长度的子字符串,如python格式
"{:10}".format("some_string")
这里有很多选择。例如使用流。
source.cpp
std::ostringstream stream;
stream << "substring";
std::string new_string = stream.str();
试试这个https://github.com/fmtlib/fmt
fmt::printf("Hello, %s!", "world"); // uses printf format string syntax
std::string s = fmt::format("{0}{1}{0}", "abra", "cad");
您可以快速编写一个简单的函数来 return 固定长度的字符串。
我们认为str字符串以null结尾,buf在调用函数之前已经定义。
void format_string(char * str, char * buf, int size)
{
for (int i=0; i<size; i++)
buf[i] = ' '; // initialize the string with spaces
int x = 0;
while (str[x])
{
if (x >= size) break;
buf[x] = str[x]; // fill up the string
}
buf[size-1] = 0; // termination char
}
用作
char buf[100];
char str[] = "Hello";
format_string(str, buf, sizeof(buf));
printf(buf);
如果您不能如上所述使用 fmt,最好的方法是使用包装器 class 进行格式化。这是我曾经做过的事情:
#include <iomanip>
#include <iostream>
class format_guard {
std::ostream& _os;
std::ios::fmtflags _f;
public:
format_guard(std::ostream& os = std::cout) : _os(os), _f(os.flags()) {}
~format_guard() { _os.flags(_f); }
};
template <typename T>
struct table_entry {
const T& entry;
int width;
table_entry(const T& entry_, int width_)
: entry(entry_), width(static_cast<int>(width_)) {}
};
template <typename T>
std::ostream& operator<<(std::ostream& os, const table_entry<T>& e) {
format_guard fg(os);
return os << std::setw(e.width) << std::right << e.entry;
}
然后您可以将其用作 std::cout << table_entry("some_string", 10)
。您可以根据需要调整 table_entry
。如果你没有 class 模板参数推导,你可以实现一个 make_table_entry
模板类型推导函数。
format_guard
是必需的,因为 std::ostream
上的一些格式选项是粘性的。
@mattn 是正确的,https://github.com/fmtlib/fmt 的 fmt 库提供了这个功能。
令人振奋的消息是这已被 C++20 标准接受。
你可以使用 fmt 库知道它在 C++20std::fmt
https://www.zverovich.net/2019/07/23/std-format-cpp20.html
https://en.cppreference.com/w/cpp/utility/format/format
在 C++20 中,您将能够使用 std::format
,它为 C++ 带来了类似于 Python 的格式:
auto s = std::format("{:10}", "some_string");
在那之前你可以使用开源{fmt} formatting library,std::format
是基于。
免责声明:我是 {fmt} 和 C++20 的作者 std::format
。
我正在寻找一种快速、简洁的方式来打印漂亮的 table 格式,单元格正确对齐。
在c++中有没有一种方便的方法来创建具有一定长度的子字符串,如python格式
"{:10}".format("some_string")
这里有很多选择。例如使用流。
source.cpp
std::ostringstream stream;
stream << "substring";
std::string new_string = stream.str();
试试这个https://github.com/fmtlib/fmt
fmt::printf("Hello, %s!", "world"); // uses printf format string syntax
std::string s = fmt::format("{0}{1}{0}", "abra", "cad");
您可以快速编写一个简单的函数来 return 固定长度的字符串。
我们认为str字符串以null结尾,buf在调用函数之前已经定义。
void format_string(char * str, char * buf, int size)
{
for (int i=0; i<size; i++)
buf[i] = ' '; // initialize the string with spaces
int x = 0;
while (str[x])
{
if (x >= size) break;
buf[x] = str[x]; // fill up the string
}
buf[size-1] = 0; // termination char
}
用作
char buf[100];
char str[] = "Hello";
format_string(str, buf, sizeof(buf));
printf(buf);
如果您不能如上所述使用 fmt,最好的方法是使用包装器 class 进行格式化。这是我曾经做过的事情:
#include <iomanip>
#include <iostream>
class format_guard {
std::ostream& _os;
std::ios::fmtflags _f;
public:
format_guard(std::ostream& os = std::cout) : _os(os), _f(os.flags()) {}
~format_guard() { _os.flags(_f); }
};
template <typename T>
struct table_entry {
const T& entry;
int width;
table_entry(const T& entry_, int width_)
: entry(entry_), width(static_cast<int>(width_)) {}
};
template <typename T>
std::ostream& operator<<(std::ostream& os, const table_entry<T>& e) {
format_guard fg(os);
return os << std::setw(e.width) << std::right << e.entry;
}
然后您可以将其用作 std::cout << table_entry("some_string", 10)
。您可以根据需要调整 table_entry
。如果你没有 class 模板参数推导,你可以实现一个 make_table_entry
模板类型推导函数。
format_guard
是必需的,因为 std::ostream
上的一些格式选项是粘性的。
@mattn 是正确的,https://github.com/fmtlib/fmt 的 fmt 库提供了这个功能。
令人振奋的消息是这已被 C++20 标准接受。
你可以使用 fmt 库知道它在 C++20std::fmt
https://www.zverovich.net/2019/07/23/std-format-cpp20.html https://en.cppreference.com/w/cpp/utility/format/format
在 C++20 中,您将能够使用 std::format
,它为 C++ 带来了类似于 Python 的格式:
auto s = std::format("{:10}", "some_string");
在那之前你可以使用开源{fmt} formatting library,std::format
是基于。
免责声明:我是 {fmt} 和 C++20 的作者 std::format
。