C++ Strings
Build a route label, inspect its byte length and replace one known character.
How it works
std::string owns a sequence of char values and releases its storage automatically. It can grow as text is appended, unlike a fixed character array. Its size counts char elements, not necessarily visible Unicode characters. Indexing requires a valid position; at offers checked access that throws on an invalid position. Use readable operations rather than manually allocating enough bytes and remembering a terminator for everyday application text. A later lesson shows how to borrow a view without extending this owning string’s lifetime.
Run it locally
Use a C++20 compiler and matching standard library in a separate lab folder. The command uses c++; substitute g++, clang++ or zig c++ for your toolchain. C++ does not currently run in Bhylu’s browser playground. Apache, PHP and MySQL are not needed.
c++ -std=c++20 -Wall -Wextra -Wpedantic -Werror strings.cpp -o lessonOn Windows use -o lesson.exe and run .\lesson.exe in PowerShell. On Unix-like systems run ./lesson. Thread examples may need -pthread on your platform.
Download the complete C++ source. Run file labs in a private empty directory, away from existing reports.
Example
#include <iostream>
#include <string>
int main() {
std::string label{"Round-"};
label += std::to_string(7);
label.at(6) = '8';
std::cout << label << "; bytes: " << label.size() << '\n';
std::cout << "Starts with Round: " << label.starts_with("Round") << '\n';
}
Expected result
Round-8; bytes: 7
Starts with Round: 1
Concatenation appends the decimal text of seven, rather than adding a character’s numeric code. The known position six holds the final digit. starts_with is a C++20 operation. Both strings and their buffers remain live through output. Text processing beyond byte-oriented identifiers needs an encoding policy and often a Unicode-aware library.
Consult the WG21 C++ working group and the C++ Core Guidelines for language references and design guidance. Check the selected standard version and your implementation’s library support.