std::string formatting like sprintf

Modern C++ makes this super simple. C++20 C++20 introduces std::format, which allows you to do exactly that. It uses replacement fields similar to those in python: Code from cppreference.com, CC BY-SA and GFDL Check out the compiler support page to see if it’s available in your standard library implementation. As of 2021-11-28, full support is only available in Visual Studio 2019 16.10, which … Read more

Is there a maxheap in the C++ standard library?

Regarding std::priority_queue: A user-provided Compare can be supplied to change the ordering, e.g. using std::greater<T> would cause the smallest element to appear as the top(). Since std::less<T> is the default template argument to the Compare template parameter, it is already a max heap by default. If you want a min heap instead (what the quote above suggest), pass std::greater<T> instead of std::less<T> as the template argument. To summarize: Max Heap: pass std::less<T> (this is the default template … Read more

How to convert an instance of std::string to lower case

Adapted from Not So Frequently Asked Questions: You’re really not going to get away without iterating through each character. There’s no way to know whether the character is lowercase or uppercase otherwise. If you really hate tolower(), here’s a specialized ASCII-only alternative that I don’t recommend you use: Be aware that tolower() can only do a per-single-byte-character substitution, which … Read more