How to use printf with std::string

C++23 Update We now finally have std::print as a way to use std::format for output directly: #include <print> #include <string> int main() { // … std::print(“Follow this command: {}”, myString); // … } This combines the best of both approaches. Original Answer It’s compiling because printf isn’t type safe, since it uses variable arguments in … Read more

Why does the standard C++ library use all lower case?

Main reason : To keep compatibility with the existing code, since they have done it with C also. Also have a look at these C++ Coding standards, which presents some generic reasoning regarding the importance of convention. These links discusses about the naming conventions of C/C++ Standard Library. Naming Convention for C API C/C++ Library … Read more

Why can’t I access elements with operator[] in a const std::map?

at() is a new method for std::map in C++11. Rather than insert a new default constructed element as operator[] does if an element with the given key does not exist, it throws a std::out_of_range exception. (This is similar to the behaviour of at() for deque and vector.) Because of this behaviour it makes sense for … Read more

Why does std::string have a length() member function in addition to size()?

As per the documentation, these are just synonyms. size() is there to be consistent with other STL containers (like vector, map, etc.) and length() is to be consistent with most peoples’ intuitive notion of character strings. People usually talk about a word, sentence or paragraph’s length, not its size, so length() is there to make … Read more