Skip to content

Stl

21.1 — The Standard Library

The C++ Standard Library is a collection of classes and functions that are part of the C++ standard. It provides highly optimized, cross-platform solutions for common tasks.

  • Goals: Efficiency, safety, and reusability.

  • Naming: Almost everything lives in the std namespace.

  • Headers: You must #include the specific header for the tool you want (e.g., <vector>, <string>, <algorithm>).


21.2 — STL Containers Overview

Containers are objects that store collections of other objects. They manage the memory for you.

  • Sequence Containers: Store data in a linear order (e.g., std::vector, std::list, std::deque).

  • Associative Containers: Store data in a sorted fashion for fast lookup (e.g., std::set, std::map).

  • Unordered Associative Containers: Use hash tables for even faster lookup, but data is not sorted (e.g., std::unordered_map).


21.3 — STL Iterators Overview

Iterators are objects that act like "pointers" to navigate through the elements of a container. They provide a uniform way to access data regardless of the container's internal structure.

  • Begin/End: container.begin() points to the first element; container.end() points to the spot just after the last element.

  • Dereferencing: Use *it to get the value the iterator is pointing to.

  • Incrementing: Use ++it to move to the next element.


21.4 — STL Algorithms Overview

Algorithms are a set of functions used to manipulate data within containers. They work via iterators, making them container-independent.

  • Common Tasks: Searching (std::find), sorting (std::sort), counting (std::count), and transforming (std::transform).

  • Efficiency: These are often more optimized than a manual for loop you might write yourself.


22.1 — std::string and std::wstring

C++ provides different string classes based on the character size needed:

  • std::string: Used for standard ASCII or UTF-8 characters (char).

  • std::wstring: Used for wide characters (wchar_t), often required for specific localized alphabets or Windows API calls.

  • Encoding: While std::string is the default, modern C++ often uses u16string or u32string for explicit Unicode support.


22.2 — std::string Construction and Destruction

Strings can be created in several ways:

  • Default: std::string s; (empty string).

  • From Literal: std::string s{ "Hello" };

  • Copy: std::string s2{ s1 };

  • Substring: std::string s{ "Hello World", 0, 5 }; // "Hello"

  • Memory: std::string manages its own memory; when the string goes out of scope, the memory is automatically freed.


22.3 — std::string Length and Capacity

There is a difference between how much text a string holds and how much memory it occupies.

  • Length/Size: s.length() or s.size() returns the number of characters currently in the string.

  • Capacity: s.capacity() returns how many characters the string can hold before it needs to reallocate more memory.

  • Shrink to fit: s.shrink_to_fit() requests the string to release unused capacity to save memory.


22.4 — Character Access and C-Style Conversion

  • Accessing Characters:

    • s[n]: Fast but does not check if n is valid (can crash).

    • s.at(n): Slower but throws an exception if n is out of bounds (safer).

  • C-Style Conversion: Many older C libraries require a char*. Use s.c_str() to get a null-terminated C-style string pointer from a std::string.


22.5 — Assignment and Swapping

  • Assignment: Use the = operator to replace the contents of a string. This handles memory reallocation automatically.

  • Swapping: s1.swap(s2) or std::swap(s1, s2) is a very fast operation. Instead of copying all characters, it simply swaps the internal pointers to the memory, making it \(O(1)\) complexity.


22.6 & 22.7 — Appending and Inserting

These methods allow you to modify the string's content dynamically:

  • Appending:

    • Use operator+= or s.append().

    • Use push_back() to add a single character to the end.

  • Inserting:

    • s.insert(index, "text"): Places "text" at the specified index, shifting existing characters to the right.

    • Note: Inserting and appending can be "expensive" operations because they may require the string to move its entire contents to a new, larger memory location.