Formatting text in C++: Old and new ways
mariusbancila.ro
Formatting text in C++: Old and new ways
1–10 of 73 posts
Re: Formatting text in C++: Old and new ways
#2 unsigned char str[]{3,4,5,6,0};
std::stringstream ss;
ss
> The content of text will be "str=♥♦♣♠".no, it wont. if you are on an old Windows with code page 437 then sure. but on any sane UTF-8 system, you're just going to get some binary data.
Re: Formatting text in C++: Old and new ways
#3Re: Formatting text in C++: Old and new ways
#4unsigned char str[]{3,4,5,6,0}; std::stringstream ss; ss > The content of text will be "str=♥♦♣♠". no, it wont. if you are on an old Windows with code page 437 then sure. but on any sane UTF-8 system, you're just going to get some binary data. 1. https://wikipedia.org/wiki/Code_page_437
Re: Formatting text in C++: Old and new ways
#5It seems so trite, especially in 2023, but please don't use sprintf. It isn't safe in general. (Even snprintf is tricky.)
Re: Formatting text in C++: Old and new ways
#6It seems so trite, especially in 2023, but please don't use sprintf. It isn't safe in general. (Even snprintf is tricky.)
Re: Formatting text in C++: Old and new ways
#7Re: Formatting text in C++: Old and new ways
#8It seems so trite, especially in 2023, but please don't use sprintf. It isn't safe in general. (Even snprintf is tricky.)
Re: Formatting text in C++: Old and new ways
#9Another example would be convenient list comprehension, convenient maps wihout juggling around with tuples, first(), second(), at()...
Re: Formatting text in C++: Old and new ways
#10I 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 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 = {
{ “one”, 1 },
{ “two”, 2 },
{ “three”, 3 }
};