4 ms·
C++20 also has an enumerate() generator, so if you like the python syntax you can just do: for (auto [i,v] : std::views::enumerate(vec)) std::cout << i << ":
by HarHarVeryFunny 4mo ago
C++20 also has an enumerate() generator, so if you like the python syntax you can just do:
for (auto [i,v] : std::views::enumerate(vec))
std::cout << i << ": " << v << std::endl;
FWIW C++23 also has a python-like print and println:
std::println("{}: {}", i, v);
- BigTTYGothGF 3mo agoThe enumerate is a better solution than the one in the blog post.
- arikrahman 3mo agoThis is the way I've been doing it and with less hiccups.
- Maxatar 3mo agostd::views::enumerate is a C++23 feature, not C++20.
- WalterBright 3mo ago> std::cout << i << ": " << v << std::endl; C++ needs to abandon iostreams. Didn't the C++ community acknowledge that it was a bad idea? In the early days of D, people did want to do a version of it for D, but I objected and currently nobody wants it.
- Matheus28 3mo agohttps://en.cppreference.com/cpp/io/print https://en.cppreference.com/cpp/io/print
- WalterBright 3mo agoThanks. Looks like it finally made C++23!
- extraduder_ire 3mo agofmt:print from fmtlib has been around for at least a decade, though having it be in the spec by default now is nice. Even in c++20, you could use format() to do most of what you'd want from it.
- pjmlp 3mo agoSome folks don't like iostreams, I keep using them, since Turbo C++ 1.0 for MS-DOS. Although I would probably use std::print in more modern compilers. D needs to work into its marketing, all key features that made it relevant back in 2011 with Andrei's book have now been copied even if badly, across all mainstream languages.
- WalterBright 3mo ago> Some folks don't like iostreams I thought it was ugly in 1987, and it hasn't improved with age. Then < > for templates made it worse. Then there's formatted I/O: void IOS_precision() { cout << "\n--------------------------\n"; cout << "Implementing ios::precision\n\n"; cout << "Implementing ios::width"; cout.setf(ios::fixed, ios::floatfield); cout.precision(2); cout<<3.1422; cout << "\n--------------------------\n"; } and I don't know if the problem with multithreading was resolved or not. > copied even if badly Any program can be written in any language. But why suffer?
- pjmlp 3mo agoThat is always given as an example of how bad it is, yet I seldom have used any kind of such formatting since 1993. iostreams hardly played a role in all C++ GUI frameworks and for object serialisation, precision flags were seldom used.