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:
-
CPPPL: Chapters 30 - 33, especially 31
CPPSL: Chapters 6, 7 - Look through
Glossary of Terms
-
Strings:
A library for managing null terminated contiguous collections of characters, via the std::basic_string<T> class. the std::string class and std::wstring class are aliases for std::basic_string<char> and std::basic_string<wchar_t>, respectively. -
Streams:
A set of libraries for sending streams of characters to and from the console/screen, files, and in-memory strings. Streams are terminated by sentinel characters and are managed via insertion and extraction operators that provide composable I/O operations. -
STL:
The Standard Template Library is a subset of the C++ standard libraries. It provides a large set of containers, each with a dedicated iterator type, and a set of algorithms designed to operate on the containers. -
Container:
Template classes designed to hold instances of some type specified by an application. Each of the containers has correct copy, move, assignment, and destruction semantics. -
Iterator:
Iterators are smart pointers, each iterator type associated with a specific container. The iterator has the ability to sequence through elements in the container without users needing to know details about the container's structure. -
Algorithms:
standard functions that operator on containers, usually accepting a starting iterator, ending iterator, and a callable object that defines what happens as each element of the container is reached during a traversal.
-
Projects:
-
Sample Project #1,
Code folder
Lexical scanner -
Sample Project #2.
Code folder
Rule-based parser -
Sample Project #3,
Code folder
Parser with Abstract Syntax Tree -
Sample Project #4,
Code folder
Code Analyzer
-
Sample Project #1,
Code folder
-
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
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:- String Helper functions - trim and split
-
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 libraryNeed to refactor these code folders Almost all of the capabilities of the streams library are demonstrated here:-
formatting I/O
Uses std::istream and std::ostream -
reading and writing files
Uses std::ifstream and std::ofstream -
reading and writing in-memory strings
Uses std::istringstream and std::ostringstream -
using filebufs to stream data
Uses std::filebuf -
reading and writing to the same filebuf
Wraps a single filebuf with both std::ifstream and std::ofstream
-
formatting I/O
-
Standard Template Library (STL)
-
STL Structure Presentation,
pptx
Summarizes the containers, iterators, and algorithms -
STL Containers Reference
cppreference.com on containers -
STL Containers Demos
Simple use of most of the containers -
STL Algorithms Reference
cppreference.com on algorithms -
STL Algorithms Demos
Demos of a few of the algorithms -
trimming strings with STL Algorithms
Useful string utilities using algorithms -
Invalid Iterators
Illustrates an issue with vector use -
Finding Duplicate Files with the help of STL containers
Illustrates power of the STL -
STL Details,
pptx
More details in reference format -
STL Odds and Ends Presentation,
pptx
How to handle a few quirks of the STL container interfaces
-
STL Structure Presentation,
pptx
-
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.
-
Lecture Take-aways:
The most important things we discussed today were: