Why is the STL so heavily based on templates instead of inheritance?

The short answer is “because C++ has moved on”. Yes, back in the late 70’s, Stroustrup intended to create an upgraded C with OOP capabilities, but that is a long time ago. By the time the language was standardized in 1998, it was no longer an OOP language. It was a multi-paradigm language. It certainly … Read more

What’s the difference between “STL” and “C++ Standard Library”?

The “STL” was written by Alexander Stepanov in the days long before C++ was standardised. C++ existed through the 80s, but what we now call “C++” is the language standardised in ISO/IEC 14882:2014 (and earlier versions, such as ISO/IEC 14882:2011). The STL was already widely used as a library for C++, giving programmers access 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

Iterator invalidation rules for C++ containers

C++03 (Source: Iterator Invalidation Rules (C++03)) Insertion Sequence containers vector: all iterators and references before the point of insertion are unaffected, unless the new container size is greater than the previous capacity (in which case all iterators and references are invalidated) [23.2.4.3/1] deque: all iterators and references are invalidated, unless the inserted member is at … 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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)