How to get IOStream to perform better?

Here is what I have gathered so far: Buffering: If by default the buffer is very small, increasing the buffer size can definitely improve the performance: it reduces the number of HDD hits it reduces the number of system calls Buffer can be set by accessing the underlying streambuf implementation. char Buffer[N]; std::ifstream file(“file.txt”); file.rdbuf()->pubsetbuf(Buffer, … Read more

Can I take the address of a function defined in standard library?

Short answer No. Explanation [namespace.std] says: Let F denote a standard library function ([global.functions]), a standard library static member function, or an instantiation of a standard library function template. Unless F is designated an addressable function, the behavior of a C++ program is unspecified (possibly ill-formed) if it explicitly or implicitly attempts to form a … Read more

Writing your own STL Container

Here’s a sequence pseudo-container I pieced together from § 23.2.1\4 Note that the iterator_category should be one of std::input_iterator_tag, std::output_iterator_tag,std::forward_iterator_tag,std::bidirectional_iterator_tag,std::random_access_iterator_tag. Also note that the below is technically more strict than required, but this is the idea. Note that the vast majority of the “standard” functions are technically optional, due to the awesomeness that is iterators. … Read more

What are the mechanics of short string optimization in libc++?

The libc++ basic_string is designed to have a sizeof 3 words on all architectures, where sizeof(word) == sizeof(void*). You have correctly dissected the long/short flag, and the size field in the short form. what value would __min_cap, the capacity of short strings, take for different architectures? In the short form, there are 3 words to … Read more

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: #include <iostream> #include <format> int main() { std::cout << std::format(“Hello {}!\n”, “world”); } Code from cppreference.com, CC BY-SA and GFDL Check out the compiler support page to see if it’s … Read more

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

Adapted from Not So Frequently Asked Questions: #include <algorithm> #include <cctype> #include <string> std::string data = “Abc”; std::transform(data.begin(), data.end(), data.begin(), [](unsigned char c){ return std::tolower(c); }); 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 … Read more