Skip to content

16 STL Containers and Algorithms

JackHeeley edited this page Mar 27, 2022 · 23 revisions

Understand and favour STL containers and algorithms to unlock the full power of the language

This is one of the key recommendations selected from the guidance literature referenced on the wiki home page. I've chosen to emphasise this specific recommendation with respect to STL because, unlike some other STL topics where you could feasibly "look it up", if you dont give some close attention to this topic, you will fail to recognize opportunities and/or fail to make proper use of one of the most powerful parts of STL.

Some examples of where, how and points of special interest are supplied in App3Dev, but not really very much. The nature of the code is such that std::string, std::vector and std::map are the only containers that are needed. Algorithms consequently aren't used heavily either.

Refer to utc_timestamp.hpp which shows erase and std::find_if in combination to effect white space trimming. Also utf8_convert.hpp count_codepoints() shows std::count_if in action to determine the number of printing characters/glyphs in a utf8 string.

Refer to utf8_guid.hpp for an example of the so-called erase-remove idiom used to delete all instances of a set of unwanted characters from a string.

Slightly off topic for this page's title, refer also to file_logger.cpp for example of a RAII way to synchronize and serialize threads using STL (using std::lockguard).

These modest examples, only hint at the power that is available in STL, but give a indication that cutting-and-pasting examples to satisfy a pressing need, is only every going to scratch the surface. Invest some training time here and get comfortable with the containers and algorithms provided in STL.