Live data from Hacker News

Formatting text in C++: Old and new ways

mariusbancila.ro

11–20 of 73 posts

Re: Formatting text in C++: Old and new ways

#12

I find it disappointing that cpp20 still doesn't have a solution that is more convenient than good ol printf (except for memory safety). Another example would be convenient list comprehension, convenient maps wihout juggling around with tuples, first(), second(), at()...

Maps have been improved quite a bit. For example, if you have a std::map , you can iterate over it like this: for (auto [s, n]: my_map) { // s = string key, n = int value } You can test for membership: if (my_map.contains(“foo”)) { /* do something * } Although I still usually use find because if the key is in the map, I probably want the value. You can use initialization lists with them too: std::map my_map = { { “on…

    for (auto [s, n]: my_map)
copies all the data needlessly, better to use

    for (const auto& [s, n]: my_map)

Re: Formatting text in C++: Old and new ways

#13

I find it disappointing that cpp20 still doesn't have a solution that is more convenient than good ol printf (except for memory safety). Another example would be convenient list comprehension, convenient maps wihout juggling around with tuples, first(), second(), at()...

What's wrong with std::format?

Re: Formatting text in C++: Old and new ways

#14

Earlier quoted context omitted.

Maps have been improved quite a bit. For example, if you have a std::map , you can iterate over it like this: for (auto [s, n]: my_map) { // s = string key, n = int value } You can test for membership: if (my_map.contains(“foo”)) { /* do something * } Although I still usually use find because if the key is in the map, I probably want the value. You can use initialization lists with them too: std::map my_map = { { “on…

for (auto [s, n]: my_map) copies all the data needlessly, better to use for (const auto& [s, n]: my_map)

what a language

Re: Formatting text in C++: Old and new ways

#15

I find it disappointing that cpp20 still doesn't have a solution that is more convenient than good ol printf (except for memory safety). Another example would be convenient list comprehension, convenient maps wihout juggling around with tuples, first(), second(), at()...

For list comprehension, we have (C++23): `std::ranges::to(items | std::views::filter(shouldInclude) | std::views::transform(f))` it’s not quite `[f(x) for x in items if shouldInclude(x)]` but it’s the same idea.

Re: Formatting text in C++: Old and new ways

#17

I find it disappointing that cpp20 still doesn't have a solution that is more convenient than good ol printf (except for memory safety). Another example would be convenient list comprehension, convenient maps wihout juggling around with tuples, first(), second(), at()...

For list comprehension, we have (C++23): `std::ranges::to (items | std::views::filter(shouldInclude) | std::views::transform(f))` it’s not quite `[f(x) for x in items if shouldInclude(x)]` but it’s the same idea.

To be honest, if that's the notation, i will not be very eager to jump on cpp23. That said, I admire people who's minds stay open for c++ improvements and make that effort.

Re: Formatting text in C++: Old and new ways

#18

I find it disappointing that cpp20 still doesn't have a solution that is more convenient than good ol printf (except for memory safety). Another example would be convenient list comprehension, convenient maps wihout juggling around with tuples, first(), second(), at()...

Maps have been improved quite a bit. For example, if you have a std::map , you can iterate over it like this: for (auto [s, n]: my_map) { // s = string key, n = int value } You can test for membership: if (my_map.contains(“foo”)) { /* do something * } Although I still usually use find because if the key is in the map, I probably want the value. You can use initialization lists with them too: std::map my_map = { { “on…

This is very useful, thanks!

Re: Formatting text in C++: Old and new ways

#19
post #8

It seems so trite, especially in 2023, but please don't use sprintf. It isn't safe in general. (Even snprintf is tricky.)

saying "dont use" something isn't really actionable, unless you give a safe alternative.

And explain why it’s not safe too
Post reply on HN