c++ - Control {fmt} formatting of nested containersranges - Stack Overflow
If I have a range I can print it with the fmt library, in this way: /8.1.0/api.html#ranges-api
#include <fmt/ranges.h>
std::vector<int> t = {1, 2, 3};
// Prints "[1, 2, 3]"
fmt::print("{}", t);
And if I want to control the separator:
// Prints "1| 2| 3"
fmt::print("{}", fmt::join(t, "| "));
What happens if I have a nested containers, for example std::vector<std::vector<int>>
, how can I control the nested separator?
std::vector<std::vector<int>> v = ...
fmt::print("{}", fmt::join(t, "| "));
this will print [1, 2, 3]| [4, 5, 6]| [7, 8, 9]
.
Note that I can only control the top level format.
For context, I am trying to control formatting of multidimensional arrays:
If I have a range I can print it with the fmt library, in this way: https://fmt.dev/8.1.0/api.html#ranges-api
#include <fmt/ranges.h>
std::vector<int> t = {1, 2, 3};
// Prints "[1, 2, 3]"
fmt::print("{}", t);
And if I want to control the separator:
// Prints "1| 2| 3"
fmt::print("{}", fmt::join(t, "| "));
What happens if I have a nested containers, for example std::vector<std::vector<int>>
, how can I control the nested separator?
std::vector<std::vector<int>> v = ...
fmt::print("{}", fmt::join(t, "| "));
this will print [1, 2, 3]| [4, 5, 6]| [7, 8, 9]
.
Note that I can only control the top level format.
For context, I am trying to control formatting of multidimensional arrays: https://godbolt./z/M9cTEox7c
Share Improve this question asked Nov 15, 2024 at 20:54 alfCalfC 16.4k4 gold badges79 silver badges154 bronze badges1 Answer
Reset to default 1You can use ranges for that:
#include <fmt/ranges.h>
#include <ranges>
#include <vector>
int main() {
std::vector<std::vector<int>> v = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
constexpr auto joinIntVec = [](auto& ve) { return fmt::join(ve, "~"); };
fmt::print("{}", fmt::join(std::views::transform(v, joinIntVec), "| "));
}
- 人工智能的温度:AI究竟会带给我们怎样的世界?
- 谷歌提出新方案 欲解决增加时间引起的计算机故障
- 成本高、厂商疑 微软Surface面临十大风险
- 电脑展趋势分析 移动终端大热DIY被弱化
- c# - Loading data to dataGridView freezes the window during loading, why threading does not work here? - Stack Overflow
- flutter - Uber category selection animation - Stack Overflow
- python - Issues with getting Tensorflow to work with RTX 4060 Laptop - Stack Overflow
- c - Dereferencing a valid pointer results in an error - Stack Overflow
- r - Elegant vectorization of nested for loop - Stack Overflow
- Why is Django Machina query altered with nonexistent table name - Stack Overflow
- python - Could NOT find Python3 during cmake - Stack Overflow
- Keeping Data In Denormalized NoSql Databases (CassandraScyllaDb) Tables In Sync? - Stack Overflow
- tensorflow - How to resolved the import error with scipy when using keras_tuner? - Stack Overflow
- python - Newton raphson fails to approximate beyond two decimal points - Stack Overflow
- How to compare dates in vba for excel - Stack Overflow
- Excel—Include LEN and HYPERLINK in One Cell - Stack Overflow
- java - Android physical keyboard support for key press and hold - Stack Overflow