I am looking for a quick and neat way to print in a nice table format with cells being aligned properly.
Is there a convenient way in c++ to create strings of substrings with certain length like python format
"{:10}".format("some_string")
I am looking for a quick and neat way to print in a nice table format with cells being aligned properly.
Is there a convenient way in c++ to create strings of substrings with certain length like python format
"{:10}".format("some_string")
In C++20 you can use std::format
which brings Python-like formatting to C++:
auto s = std::format("{:10}", "some_string");
Until it is widely available you can use the open-source {fmt} formatting library, std::format
is based on.
Disclaimer: I'm the author of {fmt} and C++20 std::format
.
Try this 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");
You have many options here. For instance using streams.
source.cpp
std::ostringstream stream;
stream << "substring";
std::string new_string = stream.str();
@mattn was correct, the fmt library at https://github.com/fmtlib/fmt provides exactly this functionality.
The exciting news is this has been accepted into the C++20 standard.
You can use the fmt library knowing that it will be std::fmt in C++20
https://www.zverovich.net/2019/07/23/std-format-cpp20.html https://en.cppreference.com/w/cpp/utility/format/format
you can quickly write a simple function to return a fixed length string.
We consider the str string is terminated by null, buf is already defined before calling the function.
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
}
Used as
char buf[100];
char str[] = "Hello";
format_string(str, buf, sizeof(buf));
printf(buf);
If you cannot use fmt as mentioned above the best way would be to use a wrapper class for formatting. Here is what I have done once:
#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;
}
And then you would use it as std::cout << table_entry("some_string", 10)
. You can adapt table_entry
to your needs. If you don't have class template argument deduction you could implement a make_table_entry
function for template type deduction.
The format_guard
is needed since some formatting options on std::ostream
are sticky.
© 2022 - 2024 — McMap. All rights reserved.