Segment 1 - STL

Advanced Programming

  • Author: Mahboob Ali

Standard Template Library

  • Core component of C++ programming

Introduction

  • C++ relies significantly on libraries that complement its core.

  • Libraries provide programming solutions to repetitive tasks in modern applications.

  • C++ libraries include:

    • Variety of types

    • Containers and algorithms for user-defined types

    • Resource management and multi-threading support

  • C++17 standard dedicates two-thirds to library support facilities.

Categories of Standard Library

  • Components divided into categories:

    • Example header files listed alongside relevant libraries.

    • Categories not covered are indicated as 'beyond scope'.

Categories and Their Components

  • Localization

    • <locale> - international text processing (beyond scope)

  • Containers

    • <vector> - part of the STL framework

  • Iterators

    • <iterator> - part of the STL framework

  • Algorithms

    • <algorithm> - part of the STL framework

  • Numerics

    • Various components for numerical operations (some beyond scope)

    • <valarray> - class for array manipulation

    • <cmath>, <ctgmath>, <cstdlib> - math operations.

  • Input/Output

    • <iostream> - primary I/O mechanism

    • Multiple base classes for I/O management (see later chapters).

    • <fstream> - file handling classes

    • <regex> - pattern matching and searching (beyond scope).

  • Atomic operations

    • <atomic> for concurrent data access (beyond scope).

  • Thread Support

    • <thread> - manages threads and communications

    • Other threading utilities (beyond scope).

String Library

  • Supports three types of strings:

    • String classes

    • String_view classes

    • C-style null-terminated string functions

String Classes

  • Character-like Data Types

    • Specializations of <std::basic_string<CharT>>:

      • std::string (char)

      • std::wstring (wchar_t)

      • std::u16string (char16_t)

      • std::u32string (char32_t)

  • Public Member Functions:

    • operator=: Assigns a string to the current string.

    • operator[]: Accesses specific characters.

    • size(): Returns number of characters.

    • substr(): Substring retrieval.

    • Various find functions to locate characters or substrings.

    • operator+=: Appends characters or strings.

  • Helper Functions:

    • operator==, operator!=: Comparisons.

    • operator>>, operator<<: Stream operations.

Standard Library Template (STL)

  • STL Overview:

    • Prominent part of C++ Standard Library.

    • Provides mechanisms for managing data structures in a generic way.

    • Comprises:

    • Container template classes

    • Sequential containers

    • Container adaptors

    • Associative containers (beyond scope)

    • Iterators

    • Algorithms

    • Function objects

Container Class Characteristics

  • Containers represent data structure shells:

    • Manage memory of elements.

    • Provide member functions for element access.

  • Iterators:

    • Allow traversal of data structures.

    • Enable simple access to ranges of elements.

  • Algorithms:

    • Provide solutions for sequences using iterators and function objects.

Reference

  • Wikipedia on C++ Standard Library

  • Wikipedia on C++ Standard Template Library

  • Read on C++ String