Week 9a - Standard Library

Strings, Streams, Composable Containers, itertors, and Algorithms

Synopsis:

This lecture is concerned with strings, streams, and the STL, all parts of the Standard C++ Libraries. It devotes much of its content to the Standard Template Library (STL), including its containers, iterators, and algorithms. We will see that callable objects and lambdas are very useful for programs that use the STL.

Readings and Activities for the Week:

Glossary of Terms

  1. Projects:

  2. Strings

    Strings are instances of the std::basic_string<T> class.
    std::string is an alias for std::basic_string<char> and std::wstring is an alias for std::basic_string<wchar_t>.
    Strings hold null terminated collections of characters, stored in contiguous memory in the native heap.
    They have all the important members of the STL classes, including members:
    • begin() returns an iterator pointing to the first character in the collection
    • end() returns an iterator pointing to one past the last character in the collection
    • default, copy construction, move construction, copy assignment, move assignment, and (non-virtual) destruction operations
    That means that a class holding only strings and perhaps STL containers, does not need to implement
    constructors, assignment, and destruction, because those compiler generated operations are correct.
    It also means that the STL algorithms work with strings. Here is an example of std::string use:
  3. Streams

    A collection of libraries for stream-based I/O, for the console, files, and in-memory strings.
    Streams use the insertion operator<<(...) and extraction operator>>(...) to build composable input and output operations.
    structure of the streams library
    Need to refactor these code folders Almost all of the capabilities of the streams library are demonstrated here:
  4. Standard Template Library (STL)

  5. Activity to prepare for Synchronous Discussion:

    • Find at least one place in your Project #3 or #4 code that loops through a collection, making modifications or collecting information from the elements. Replace that code with an STL Algorithm that accepts start and end iterators, and a callable object. You define code for the callable object to replace your original operations.
  6. Lecture Take-aways:

    The most important things we discussed today were: