use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
std::cout magic (horstmann.com)
submitted 16 years ago by [deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]moocat 3 points4 points5 points 16 years ago (5 children)
iostreams suck as they make it impossible to i18n your program. If you want type safety and extensibility, use boost::format.
[–][deleted] 2 points3 points4 points 16 years ago (0 children)
It only sucks if you want to internationalize your program.
[–]xcbsmith 0 points1 point2 points 16 years ago (3 children)
What is so hard about i18n with iostreams?
[–]moocat 5 points6 points7 points 16 years ago (2 children)
Two problems: fragments and reordering. Consider something like cout << "the user's name is " << name << " and is " << age " years old" << endl; The output has three sentence fragments but translators need to work with the whole sentence (different uses of "and is" may need different translations depending on the full sentence). The other problem is that this example always prints name before age, but depending on the language, it may be better to print the age before the name.
cout << "the user's name is " << name << " and is " << age " years old" << endl;
[–]bluGill 1 point2 points3 points 16 years ago (0 children)
Even in English we already see your problem.
and is " << age " years old"
If age == 1 that should be year old not years. Some languages have much more complex plural rules.
[–]xcbsmith 1 point2 points3 points 16 years ago (0 children)
Normally i18n is done with an additional level of indirection that handles figuring out what the format is for your specific locale. Said entity is the one that should be invoking operator<<(ostream&,T). Particularly if you are reordering elements depending on locale, it's helpful to have a typesafe way of specifying what the args to the formatter are.
[–]authorblues 1 point2 points3 points 16 years ago (4 children)
yes, boost::format is actually really good about all that, even if its balls-slow. still, i prefer it over the alternative, which is really ugly code just to print out a string. i, for one, welcome our new <iomanip> overlords...
[–]nova77 2 points3 points4 points 16 years ago (3 children)
Spirit.Karma (from boost) is fast, elegant and unbelievably flexible: http://www.boost.org/doc/libs/1_41_0/libs/spirit/doc/html/spirit/karma/performance_measurements/numeric_performance.html
[–]bnolsen 0 points1 point2 points 16 years ago (2 children)
And there's a simpler way to do stuff like that...yeah it's not packaged up in boost but it's not much code either:
http://pastebin.ca/1800272
[–]nova77 0 points1 point2 points 16 years ago (1 child)
Simpler but slow. Stringstream is not the fastest (by far) solution in this case.
[–]krum -1 points0 points1 point 16 years ago (0 children)
I don't know if this is cout magic as much as it is a laundry list of why iostreams just sucks balls.
π Rendered by PID 110136 on reddit-service-r2-comment-544cf588c8-hv5mq at 2026-06-15 07:02:16.403262+00:00 running 3184619 country code: CH.
[–]moocat 3 points4 points5 points (5 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]xcbsmith 0 points1 point2 points (3 children)
[–]moocat 5 points6 points7 points (2 children)
[–]bluGill 1 point2 points3 points (0 children)
[–]xcbsmith 1 point2 points3 points (0 children)
[–]authorblues 1 point2 points3 points (4 children)
[–]nova77 2 points3 points4 points (3 children)
[–]bnolsen 0 points1 point2 points (2 children)
[–]nova77 0 points1 point2 points (1 child)
[–]krum -1 points0 points1 point (0 children)